ModuleBuilder.DefineGlobalMethod 方法

定义

定义全局方法。

重载

名称 说明
DefineGlobalMethod(String, MethodAttributes, CallingConventions, Type, Type[])

定义具有指定名称、属性、调用约定、返回类型和参数类型的全局方法。

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

定义具有指定名称、属性、调用约定、返回类型、返回类型的自定义修饰符、参数类型以及参数类型的自定义修饰符的全局方法。

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

定义具有指定名称、属性、返回类型和参数类型的全局方法。

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

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
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=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[][])

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
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

以前 CreateGlobalFunctions() 已调用该方法。

注解

此重载是为托管编译器的设计器提供的。

在调用 CreateGlobalFunctions之前,不能使用此方法定义的全局方法。

适用于

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

Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
ModuleBuilder.cs
Source:
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=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之前不可用。

适用于