ModuleBuilder.DefineGlobalMethod 方法

定義

定義全域方法。

多載

DefineGlobalMethod(String, MethodAttributes, Type, Type[])

使用指定的名稱、屬性、傳回型別和參數型別,來定義全域方法。

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])

使用指定的名稱、屬性、呼叫慣例、傳回型別和參數型別,來定義全域方法。

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

使用指定的名稱、屬性、呼叫慣例、傳回型別、傳回型別的自訂修飾詞、參數型別以及參數型別的自訂修飾詞,來定義全域方法。

DefineGlobalMethod(String, MethodAttributes, Type, Type[])

來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs

使用指定的名稱、屬性、傳回型別和參數型別,來定義全域方法。

public:
 System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, Type returnType, Type[] parameterTypes);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, returnType As Type, parameterTypes As Type()) As MethodBuilder

參數

name
String

方法的名稱。 name 不能包含內嵌的 null。

attributes
MethodAttributes

方法的屬性。 attributes 必須包括 Static

returnType
Type

方法的傳回類型。

parameterTypes
Type[]

方法參數的類型。

傳回

已定義的全域方法。

例外狀況

這個方法不是靜態的。 也就是說,attributes 不包括 Static

-或-

name 的長度為零。

-或-

Type 陣列中的元素為 null

namenull

範例

下列範例說明 如何使用 DefineGlobalMethod 來建立系結至目前 ModuleBuilder的型別獨立方法。 建置全域方法之後, CreateGlobalFunctions 必須呼叫 才能完成。

AppDomain^ currentDomain;
AssemblyName^ myAssemblyName;
MethodBuilder^ myMethodBuilder = nullptr;
ILGenerator^ myILGenerator;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(
      myAssemblyName, AssemblyBuilderAccess::RunAndSave );

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule" );

// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder->DefineGlobalMethod(
   "MyMethod1", (MethodAttributes)(MethodAttributes::Static | MethodAttributes::Public),
   nullptr, nullptr );
myILGenerator = myMethodBuilder->GetILGenerator();
myILGenerator->EmitWriteLine( "Hello World from global method." );
myILGenerator->Emit( OpCodes::Ret );

// Fix up the 'TempModule' module .
myModuleBuilder->CreateGlobalFunctions();
AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;
ILGenerator myILGenerator;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod
     ("MyMethod1",MethodAttributes.Static|MethodAttributes.Public,
           null,null);
myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.EmitWriteLine("Hello World from global method.");
myILGenerator.Emit(OpCodes.Ret);
// Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions();
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing
Dim myILGenerator As ILGenerator

' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"

' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
                                                   AssemblyBuilderAccess.RunAndSave)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")

' Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod("MyMethod1", MethodAttributes.Static _
                                          Or MethodAttributes.Public, Nothing, Nothing)
myILGenerator = myMethodBuilder.GetILGenerator()
myILGenerator.EmitWriteLine("Hello World from global method.")
myILGenerator.Emit(OpCodes.Ret)
' Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions()

備註

在您呼叫 CreateGlobalFunctions之前,此方法定義的全域方法無法使用。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])

來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs

使用指定的名稱、屬性、呼叫慣例、傳回型別和參數型別,來定義全域方法。

public:
 System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As MethodBuilder

參數

name
String

方法的名稱。 name 不能包含內嵌的 null。

attributes
MethodAttributes

方法的屬性。 attributes 必須包括 Static

callingConvention
CallingConventions

方法的呼叫慣例。

returnType
Type

方法的傳回類型。

parameterTypes
Type[]

方法參數的類型。

傳回

已定義的全域方法。

例外狀況

這個方法不是靜態的。 也就是說,attributes 不包括 Static

-或-

Type 陣列中的元素為 null

namenull

範例

下列程式代碼範例說明如何使用 DefineGlobalMethod 來建立系結至目前 ModuleBuilder的型別獨立方法。 建置全域方法之後, CreateGlobalFunctions 必須呼叫 才能完成。

AppDomain^ currentDomain;
AssemblyName^ myAssemblyName;
MethodBuilder^ myMethodBuilder = nullptr;
ILGenerator^ myILGenerator;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(
      myAssemblyName, AssemblyBuilderAccess::RunAndSave );

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule( "TempModule" );

// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder->DefineGlobalMethod(
   "MyMethod1", (MethodAttributes)(MethodAttributes::Static | MethodAttributes::Public),
   nullptr, nullptr );
myILGenerator = myMethodBuilder->GetILGenerator();
myILGenerator->EmitWriteLine( "Hello World from global method." );
myILGenerator->Emit( OpCodes::Ret );

// Fix up the 'TempModule' module .
myModuleBuilder->CreateGlobalFunctions();
AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;
ILGenerator myILGenerator;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder =
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod
     ("MyMethod1",MethodAttributes.Static|MethodAttributes.Public,
           null,null);
myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.EmitWriteLine("Hello World from global method.");
myILGenerator.Emit(OpCodes.Ret);
// Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions();
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing
Dim myILGenerator As ILGenerator

' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"

' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName, _
                                                   AssemblyBuilderAccess.RunAndSave)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")

' Define a global method in the 'TempModule' module.
myMethodBuilder = myModuleBuilder.DefineGlobalMethod("MyMethod1", MethodAttributes.Static _
                                          Or MethodAttributes.Public, Nothing, Nothing)
myILGenerator = myMethodBuilder.GetILGenerator()
myILGenerator.EmitWriteLine("Hello World from global method.")
myILGenerator.Emit(OpCodes.Ret)
' Fix up the 'TempModule' module .
myModuleBuilder.CreateGlobalFunctions()

備註

在呼叫 CreateGlobalFunctions之前,您無法使用這個方法所定義的全域方法。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於

DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs
來源:
ModuleBuilder.cs

使用指定的名稱、屬性、呼叫慣例、傳回型別、傳回型別的自訂修飾詞、參數型別以及參數型別的自訂修飾詞,來定義全域方法。

public:
 System::Reflection::Emit::MethodBuilder ^ DefineGlobalMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ requiredReturnTypeCustomModifiers, cli::array <Type ^> ^ optionalReturnTypeCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredParameterTypeCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalParameterTypeCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? requiredReturnTypeCustomModifiers, Type[]? optionalReturnTypeCustomModifiers, Type[]? parameterTypes, Type[][]? requiredParameterTypeCustomModifiers, Type[][]? optionalParameterTypeCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineGlobalMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers);
member this.DefineGlobalMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.MethodBuilder
Public Function DefineGlobalMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, requiredReturnTypeCustomModifiers As Type(), optionalReturnTypeCustomModifiers As Type(), parameterTypes As Type(), requiredParameterTypeCustomModifiers As Type()(), optionalParameterTypeCustomModifiers As Type()()) As MethodBuilder

參數

name
String

方法的名稱。 name 不能含有內嵌 null 字元。

attributes
MethodAttributes

方法的屬性。 attributes 必須包括 Static

callingConvention
CallingConventions

方法的呼叫慣例。

returnType
Type

方法的傳回類型。

requiredReturnTypeCustomModifiers
Type[]

型別的陣列,表示傳回型別的必要自訂修飾詞,例如 IsConstIsBoxed。 如果傳回類型沒有必要的自訂修飾詞,則指定 null

optionalReturnTypeCustomModifiers
Type[]

型別的陣列,表示傳回型別的選擇性自訂修飾詞,例如 IsConstIsBoxed。 如果傳回類型沒有選擇性的自訂修飾詞,則指定 null

parameterTypes
Type[]

方法參數的類型。

requiredParameterTypeCustomModifiers
Type[][]

類型陣列的陣列。 每一個型別陣列都表示全域方法之對應參數的必要自訂修飾詞。 如果特定引數沒有必要的自訂修飾詞,請指定 null,而不要指定型別的陣列。 如果全域方法沒有任何引數,或者沒有任何引數具有必要的自訂修飾詞,請指定 null,而不要指定陣列的陣列。

optionalParameterTypeCustomModifiers
Type[][]

類型陣列的陣列。 每一個型別陣列都表示對應參數的選擇性自訂修飾詞。 如果特定引數沒有選擇性的自訂修飾詞,請指定 null,而不要指定型別的陣列。 如果全域方法沒有任何引數,或者沒有任何引數具有選擇性自訂修飾詞,請指定 null,而不要指定陣列的陣列。

傳回

已定義的全域方法。

例外狀況

這個方法不是靜態的。 也就是說,attributes 不包括 Static

-或-

Type 陣列中的元素為 null

namenull

備註

此多載是針對 Managed 編譯程式的設計工具所提供。

在呼叫 CreateGlobalFunctions之前,您無法使用這個方法所定義的全域方法。

注意

從 .NET Framework 2.0 Service Pack 1 開始,此成員不再需要 ReflectionPermissionReflectionPermissionFlag.ReflectionEmit標。 (請參閱反映發出中的安全性問題.) 若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於