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[])

使用指定的名称、属性、返回类型和参数类型定义一个全局方法。

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[]

方法参数的类型。

返回

MethodBuilder

已定义的全局方法。

例外

此方法不是静态的。 也就是说,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[])

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

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[]

方法参数的类型。

返回

MethodBuilder

已定义的全局方法。

例外

此方法不是静态的。 也就是说,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[][])

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

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,而不要指定由数组组成的数组。

返回

MethodBuilder

已定义的全局方法。

例外

此方法不是静态的。 也就是说,attributes 不包括 Static

  • 或 - Type 数组中的一个元素为 null

namenull

注解

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

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

备注

从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要 ReflectionPermission 带有 ReflectionPermissionFlag.ReflectionEmit 标志的。 (参阅反射发出中的安全问题。 ) 若要使用此功能,你的应用程序应面向 .NET Framework 3.5 或更高版本。

适用于