ModuleBuilder.DefineGlobalMethod 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義了一種全域方法。
多載
| 名稱 | Description |
|---|---|
| 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[])
定義一個全域方法,包含指定的名稱、屬性、回傳類型及參數類型。
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);
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 無法包含嵌入的空。
- attributes
- MethodAttributes
方法的屬性。
attributes 必須包含 Static。
- returnType
- Type
方法是方法的回傳類型。
- parameterTypes
- Type[]
方法參數的類型。
傳回
定義的全域方法。
例外狀況
name 是 null。
範例
以下範例說明如何利用 來 DefineGlobalMethod 建立一種與當前 ModuleBuilder綁定為 的型別無關方法。 建構全域方法後,必須呼叫 才能 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之前無法使用。
適用於
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])
定義一個全域方法,包含指定的名稱、屬性、呼叫慣例、回傳型別及參數型別。
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);
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 無法包含嵌入的空。
- attributes
- MethodAttributes
方法的屬性。
attributes 必須包含 Static。
- callingConvention
- CallingConventions
方法的呼叫慣例。
- returnType
- Type
方法是方法的回傳類型。
- parameterTypes
- Type[]
方法參數的類型。
傳回
定義的全域方法。
例外狀況
name 是 null。
範例
以下程式碼範例說明如何利用 來 DefineGlobalMethod 建立與目前 ModuleBuilder綁定的型別無關方法。 建構全域方法後,必須呼叫 才能 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。
適用於
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])
定義一個全域方法,包含指定的名稱、屬性、呼叫慣例、回傳類型、返回類型的自訂修飾符、參數類型,以及參數類型的自訂修飾符。
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);
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 無法包含嵌入的空字元。
- attributes
- MethodAttributes
方法的屬性。
attributes 必須包含 Static。
- callingConvention
- CallingConventions
方法的呼叫慣例。
- returnType
- Type
方法是方法的回傳類型。
- requiredReturnTypeCustomModifiers
- Type[]
一個代表回傳類型所需的自訂修飾符的型別陣列,例如 IsConst 或 IsBoxed。 若返回類型沒有必要的自訂修飾符,請指定 null。
- optionalReturnTypeCustomModifiers
- Type[]
一組代表回傳類型可選自訂修飾符的陣列,例如 IsConst 或 IsBoxed。 若返回類型沒有可選的自訂修飾符,請指定 null。
- parameterTypes
- Type[]
方法參數的類型。
- requiredParameterTypeCustomModifiers
- Type[][]
一組類型的陣列。 每個類型陣列代表全域方法對應參數所需的自訂修飾符。 如果某個參數沒有必要的自訂修飾符,則指定 null 為 代替型別陣列。 如果全域方法沒有參數,或沒有任何參數需要自訂修飾符,則以指定 null 代替陣列的陣列。
- optionalParameterTypeCustomModifiers
- Type[][]
一組類型的陣列。 每個型別陣列代表對應參數的可選自訂修飾符。 如果某個參數沒有可選的自訂修飾符,則指定 null 為 ,而不是一組型別。 如果全域方法沒有參數,或所有參數都沒有可選的自訂修飾符,則以 Specified null 代替陣列的陣列。
傳回
定義的全域方法。
例外狀況
name 是 null。
此 CreateGlobalFunctions() 方法過去曾被稱為
備註
這種過載是為受管編譯器設計者提供的。
你無法使用此方法定義的全域方法,除非你呼叫 CreateGlobalFunctions。