TypeBuilder.DefineMethod 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向类型添加一个方法。
重载
DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][]) |
使用指定名称、方法属性、调用约定、方法签名和自定义修饰符向类型中添加新方法。 |
DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[]) |
使用指定的名称、方法属性、调用约定和方法签名向类型中添加新方法。 |
DefineMethod(String, MethodAttributes, CallingConventions) |
将具有指定的名称、 方法属性和调用约定的新方法添加到此类型。 |
DefineMethod(String, MethodAttributes) |
向此类型添加新方法,使用指定的名称和方法属性。 |
DefineMethod(String, MethodAttributes, Type, Type[]) |
向此类型添加新方法,并指定方法的名称、 属性和签名。 |
DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
使用指定名称、方法属性、调用约定、方法签名和自定义修饰符向类型中添加新方法。
public:
System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()()) As MethodBuilder
参数
- name
- String
方法的名称。
name
不能包含嵌入的 null。
- attributes
- MethodAttributes
方法的属性。
- callingConvention
- CallingConventions
方法的调用约定。
- returnType
- Type
方法的返回类型。
- returnTypeRequiredCustomModifiers
- Type[]
一个类型数组,表示该方法的返回类型所必需的自定义修饰符,如 IsConst。 如果返回类型没有所需的自定义修饰符,则指定 null
。
- returnTypeOptionalCustomModifiers
- Type[]
一个类型数组,表示该方法的返回类型的可选自定义修饰符,如 IsConst。 如果返回类型没有可选的自定义修饰符,则指定 null
。
- parameterTypes
- Type[]
方法参数的类型。
- parameterTypeRequiredCustomModifiers
- Type[][]
由类型数组组成的数组。 每个类型数组均表示相应参数所必需的自定义修饰符,如 IsConst。 如果某个特定参数没有所需的自定义修饰符,则指定 null
,而不要指定类型数组。 如果所有参数都没有所需的自定义修饰符,则指定 null
,而不要指定由数组组成的数组。
- parameterTypeOptionalCustomModifiers
- Type[][]
由类型数组组成的数组。 每个类型数组均表示相应参数的可选自定义修饰符,如 IsConst。 如果某个特定参数没有可选的自定义修饰符,则指定 null
,而不要指定类型数组。 如果所有参数都没有可选的自定义修饰符,则指定 null
,而不要指定由数组组成的数组。
返回
一个表示新添加方法的 MethodBuilder 对象。
例外
name
的长度为零。
- 或 -
此方法的父类型是一个接口,并且此方法不是虚方法(Visual Basic 中为 Overridable
)。
- 或 -
parameterTypeRequiredCustomModifiers
或 parameterTypeOptionalCustomModifiers
的大小与 parameterTypes
的大小不相等。
name
为 null
。
该类型是以前使用 CreateType() 创建的。
- 或 -
对于当前的动态类型,属性 IsGenericType 的值为 true
,但属性 IsGenericTypeDefinition 的值为 false
。
注解
如果需要指定自定义修饰符,请使用此重载。 如果需要在创建方法后指定自定义修饰符(例如,对于参数类型由其泛型类型参数指定的泛型方法),可以使用 DefineMethod(String, MethodAttributes) 或 DefineMethod(String, MethodAttributes, CallingConventions) 方法重载来定义方法,然后使用 MethodBuilder.SetSignature 方法定义参数,并使用自定义修饰符返回类型。
注意
有关自定义修饰符的详细信息,请参阅 ECMA C# 和公共语言基础结构标准 以及 标准 ECMA-335 - 公共语言基础结构 (CLI) 。
适用于
DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[])
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
使用指定的名称、方法属性、调用约定和方法签名向类型中添加新方法。
public:
System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As MethodBuilder
参数
- name
- String
方法的名称。
name
不能包含嵌入的 null。
- attributes
- MethodAttributes
方法的属性。
- callingConvention
- CallingConventions
方法的调用约定。
- returnType
- Type
方法的返回类型。
- parameterTypes
- Type[]
方法参数的类型。
返回
表示新定义的方法的 MethodBuilder。
例外
name
为 null
。
该类型是以前使用 CreateType() 创建的。
- 或 -
对于当前的动态类型,属性 IsGenericType 的值为 true
,但属性 IsGenericTypeDefinition 的值为 false
。
示例
下面的代码示例演示如何使用 DefineMethod
在动态类型上设置构造函数的特定签名和属性,并返回对应于 MethodBuilder MSIL 填充的 。
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public interface class IMyInterface
{
String^ HelloMethod( String^ parameter );
};
public ref class EmittedClass
{
public:
// Because this method calls Activator::CreateInstance,
// it requires full trust.
[System::Security::Permissions::PermissionSetAttribute
(System::Security::Permissions::SecurityAction::Demand, Name = "FullTrust")]
static void Main()
{
Type^ myNestedClassType = CreateCallee( Thread::GetDomain() );
// Create an instance of 'MyNestedClass'.
IMyInterface^ myInterface = dynamic_cast<IMyInterface^>(Activator::CreateInstance( myNestedClassType ));
Console::WriteLine( myInterface->HelloMethod( "Bill" ) );
}
private:
// Create the callee transient dynamic assembly.
static Type^ CreateCallee( AppDomain^ myAppDomain )
{
AssemblyName^ myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "EmittedClass";
// Create the callee dynamic assembly.
AssemblyBuilder^ myAssembly = myAppDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );
// Create a dynamic module in the callee assembly.
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule" );
// Define a public class named "MyHelloWorld".
TypeBuilder^ myHelloWorldType = myModule->DefineType( "MyHelloWorld", TypeAttributes::Public );
// Define a public nested class named 'MyNestedClass'.
array<Type^>^temp0 = {IMyInterface::typeid};
TypeBuilder^ myNestedClassType = myHelloWorldType->DefineNestedType( "MyNestedClass", TypeAttributes::NestedPublic, EmittedClass::typeid, temp0 );
// Implement 'IMyInterface' interface.
myNestedClassType->AddInterfaceImplementation( IMyInterface::typeid );
// Define 'HelloMethod' of 'IMyInterface'.
array<Type^>^temp1 = {String::typeid};
MethodBuilder^ myHelloMethod = myNestedClassType->DefineMethod( "HelloMethod", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Virtual), String::typeid, temp1 );
// Generate IL for 'GetGreeting' method.
ILGenerator^ myMethodIL = myHelloMethod->GetILGenerator();
myMethodIL->Emit( OpCodes::Ldstr, "Hi! " );
myMethodIL->Emit( OpCodes::Ldarg_1 );
array<Type^>^temp2 = {String::typeid,String::typeid};
MethodInfo^ infoMethod = String::typeid->GetMethod( "Concat", temp2 );
myMethodIL->Emit( OpCodes::Call, infoMethod );
myMethodIL->Emit( OpCodes::Ret );
MethodInfo^ myHelloMethodInfo = IMyInterface::typeid->GetMethod( "HelloMethod" );
// Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType->DefineMethodOverride( myHelloMethod, myHelloMethodInfo );
// Create 'MyHelloWorld' type.
Type^ myType = myHelloWorldType->CreateType();
// Create 'MyNestedClass' type.
return myNestedClassType->CreateType();
}
};
int main()
{
EmittedClass::Main();
}
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
public interface IMyInterface
{
String HelloMethod(String parameter);
}
public class Example
{
public static void Main()
{
Type myNestedClassType = CreateCallee(Thread.GetDomain());
// Cretae an instance of 'MyNestedClass'.
IMyInterface myInterface =
(IMyInterface)Activator.CreateInstance(myNestedClassType);
Console.WriteLine(myInterface.HelloMethod("Bill"));
}
// Create the callee transient dynamic assembly.
private static Type CreateCallee(AppDomain myAppDomain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "Example";
// Create the callee dynamic assembly.
AssemblyBuilder myAssembly =
myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
// Create a dynamic module in the callee assembly.
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
// Define a public class named "MyHelloWorld".
TypeBuilder myHelloWorldType =
myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
// Define a public nested class named 'MyNestedClass'.
TypeBuilder myNestedClassType =
myHelloWorldType.DefineNestedType("MyNestedClass",
TypeAttributes.NestedPublic, typeof(Example),
new Type[]{typeof(IMyInterface)});
// Implement 'IMyInterface' interface.
myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
// Define 'HelloMethod' of 'IMyInterface'.
MethodBuilder myHelloMethod =
myNestedClassType.DefineMethod("HelloMethod",
MethodAttributes.Public | MethodAttributes.Virtual,
typeof(String), new Type[]{typeof(String)});
// Generate IL for 'GetGreeting' method.
ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
myMethodIL.Emit(OpCodes.Ldarg_1);
MethodInfo infoMethod =
typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
myMethodIL.Emit(OpCodes.Call, infoMethod);
myMethodIL.Emit(OpCodes.Ret);
MethodInfo myHelloMethodInfo =
typeof(IMyInterface).GetMethod("HelloMethod");
// Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
// Create 'MyHelloWorld' type.
Type myType = myHelloWorldType.CreateType();
// Create 'MyNestedClass' type.
return myNestedClassType.CreateType();
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions
Public Interface IMyInterface
Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface
Public Class Example
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Shared Sub Main()
Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
' Create an instance of 'MyNestedClass'.
Dim myInterface As IMyInterface = _
CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
Console.WriteLine(myInterface.HelloMethod("Bill"))
End Sub
' Create the callee transient dynamic assembly.
Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "Example"
' Create the callee dynamic assembly.
Dim myAssembly As AssemblyBuilder = _
myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
' Create a dynamic module in the callee assembly.
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "MyHelloWorld".
Dim myHelloWorldType As TypeBuilder = _
myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
' Define a public nested class named 'MyNestedClass'.
Dim myNestedClassType As TypeBuilder = _
myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
GetType(Example), New Type() {GetType(IMyInterface)})
' Implement 'IMyInterface' interface.
myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
' Define 'HelloMethod' of 'IMyInterface'.
Dim myHelloMethod As MethodBuilder = _
myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
' Generate IL for 'GetGreeting' method.
Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
myMethodIL.Emit(OpCodes.Ldarg_1)
Dim infoMethod As MethodInfo = _
GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
myMethodIL.Emit(OpCodes.Call, infoMethod)
myMethodIL.Emit(OpCodes.Ret)
Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
' Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
' Create 'MyHelloWorld' type.
Dim myType As Type = myHelloWorldType.CreateType()
' Create 'MyNestedClass' type.
Return myNestedClassType.CreateType()
End Function 'CreateCallee
End Class
适用于
DefineMethod(String, MethodAttributes, CallingConventions)
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
将具有指定的名称、 方法属性和调用约定的新方法添加到此类型。
public:
System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions) As MethodBuilder
参数
- name
- String
方法的名称。
name
不能包含嵌入的 null。
- attributes
- MethodAttributes
方法的属性。
- callingConvention
- CallingConventions
方法的调用约定。
返回
表示新定义的方法的 MethodBuilder。
例外
name
为 null
。
该类型是以前使用 CreateType() 创建的。
- 或 -
对于当前的动态类型,属性 IsGenericType 的值为 true
,但属性 IsGenericTypeDefinition 的值为 false
。
注解
如果在定义方法时不知道方法签名,请使用此方法重载。 例如,泛型方法的参数类型和返回类型可能由方法的泛型类型参数指定,这些参数必须在方法添加到类型后定义。 稍后 MethodBuilder.SetSignature 可以使用 方法设置方法的参数和返回类型。
另请参阅
适用于
DefineMethod(String, MethodAttributes)
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
向此类型添加新方法,使用指定的名称和方法属性。
public:
System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes);
member this.DefineMethod : string * System.Reflection.MethodAttributes -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes) As MethodBuilder
参数
- name
- String
方法的名称。
name
不能包含嵌入的 null。
- attributes
- MethodAttributes
方法的属性。
返回
表示新定义的方法的 MethodBuilder。
例外
name
为 null
。
该类型是以前使用 CreateType() 创建的。
- 或 -
对于当前的动态类型,属性 IsGenericType 的值为 true
,但属性 IsGenericTypeDefinition 的值为 false
。
示例
下面的代码示例定义了一个名为 DemoMethod
的泛型方法,其参数类型和返回类型由其泛型类型参数指定。 使用标准调用约定在不使用签名的情况下定义 方法。 方法 MethodBuilder.DefineGenericParameters 用于生成 DemoMethod
泛型方法,然后将新定义的类型参数用于签名和返回类型。
此代码示例是为 方法提供的更大示例的 DefineGenericParameters 一部分。
// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for
// those types, and the type parameters have not been
// defined yet.
MethodBuilder^ sampleMethodBuilder =
sampleTypeBuilder->DefineMethod("SampleMethod",
MethodAttributes::Public | MethodAttributes::Static);
// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for
// those types, and the type parameters have not been
// defined yet.
MethodBuilder demoMethod = demoType.DefineMethod(
"DemoMethod",
MethodAttributes.Public | MethodAttributes.Static
);
' Define a Shared, Public method with standard calling
' conventions. Do not specify the parameter types or the
' return type, because type parameters will be used for
' those types, and the type parameters have not been
' defined yet.
Dim demoMethod As MethodBuilder = _
demoType.DefineMethod("DemoMethod", _
MethodAttributes.Public Or MethodAttributes.Static)
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are
// single alphabetic characters. T and U are used here.
//
array<String^>^ genericTypeNames = {"T", "U"};
array<GenericTypeParameterBuilder^>^ genericTypes =
sampleMethodBuilder->DefineGenericParameters(
genericTypeNames);
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = {"T", "U"};
GenericTypeParameterBuilder[] typeParameters =
demoMethod.DefineGenericParameters(typeParamNames);
// The second type parameter is constrained to be a
// reference type.
typeParameters[1].SetGenericParameterAttributes(
GenericParameterAttributes.ReferenceTypeConstraint);
' Defining generic parameters for the method makes it a
' generic method. By convention, type parameters are
' single alphabetic characters. T and U are used here.
'
Dim typeParamNames() As String = {"T", "U"}
Dim typeParameters() As GenericTypeParameterBuilder = _
demoMethod.DefineGenericParameters(typeParamNames)
' The second type parameter is constrained to be a
' reference type.
typeParameters(1).SetGenericParameterAttributes( _
GenericParameterAttributes.ReferenceTypeConstraint)
// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
array<Type^>^ parameterTypes = {genericTypes[0]};
sampleMethodBuilder->SetParameters(parameterTypes);
// Set the return type for the method. The return type is
// specified by the second type parameter, U.
sampleMethodBuilder->SetReturnType(genericTypes[1]);
// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = {typeParameters[0]};
demoMethod.SetParameters(parms);
// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);
' Set parameter types for the method. The method takes
' one parameter, and its type is specified by the first
' type parameter, T.
Dim params() As Type = {typeParameters(0)}
demoMethod.SetParameters(params)
' Set the return type for the method. The return type is
' specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters(1))
注解
如果在定义方法时不知道方法签名,请使用此方法重载。 例如,泛型方法的参数类型和返回类型可能由方法的泛型类型参数指定,这些参数必须在方法添加到类型后定义。 稍后 MethodBuilder.SetSignature 可以使用 方法设置方法的参数和返回类型。
此方法重载使用 定义方法 CallingConventions.Standard。 如果需要使用其他调用约定定义没有签名的方法,请使用 DefineMethod(String, MethodAttributes, CallingConventions) 方法重载。
另请参阅
适用于
DefineMethod(String, MethodAttributes, Type, Type[])
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
- Source:
- TypeBuilder.cs
向此类型添加新方法,并指定方法的名称、 属性和签名。
public:
System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod (string name, System.Reflection.MethodAttributes attributes, Type returnType, Type[] parameterTypes);
member this.DefineMethod : string * System.Reflection.MethodAttributes * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, returnType As Type, parameterTypes As Type()) As MethodBuilder
参数
- name
- String
方法的名称。
name
不能包含嵌入的 null。
- attributes
- MethodAttributes
方法的属性。
- returnType
- Type
方法的返回类型。
- parameterTypes
- Type[]
方法参数的类型。
返回
已定义的方法。
例外
name
为 null
。
该类型是以前使用 CreateType() 创建的。
- 或 -
对于当前的动态类型,属性 IsGenericType 的值为 true
,但属性 IsGenericTypeDefinition 的值为 false
。
示例
下面的代码示例演示了如何使用 DefineMethod
在动态类型上设置构造函数的特定签名和属性,并返回 MethodBuilder 对应于 MSIL 填充的 。
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
public interface class IMyInterface
{
String^ HelloMethod( String^ parameter );
};
public ref class EmittedClass
{
public:
// Because this method calls Activator::CreateInstance,
// it requires full trust.
[System::Security::Permissions::PermissionSetAttribute
(System::Security::Permissions::SecurityAction::Demand, Name = "FullTrust")]
static void Main()
{
Type^ myNestedClassType = CreateCallee( Thread::GetDomain() );
// Create an instance of 'MyNestedClass'.
IMyInterface^ myInterface = dynamic_cast<IMyInterface^>(Activator::CreateInstance( myNestedClassType ));
Console::WriteLine( myInterface->HelloMethod( "Bill" ) );
}
private:
// Create the callee transient dynamic assembly.
static Type^ CreateCallee( AppDomain^ myAppDomain )
{
AssemblyName^ myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "EmittedClass";
// Create the callee dynamic assembly.
AssemblyBuilder^ myAssembly = myAppDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Run );
// Create a dynamic module in the callee assembly.
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule" );
// Define a public class named "MyHelloWorld".
TypeBuilder^ myHelloWorldType = myModule->DefineType( "MyHelloWorld", TypeAttributes::Public );
// Define a public nested class named 'MyNestedClass'.
array<Type^>^temp0 = {IMyInterface::typeid};
TypeBuilder^ myNestedClassType = myHelloWorldType->DefineNestedType( "MyNestedClass", TypeAttributes::NestedPublic, EmittedClass::typeid, temp0 );
// Implement 'IMyInterface' interface.
myNestedClassType->AddInterfaceImplementation( IMyInterface::typeid );
// Define 'HelloMethod' of 'IMyInterface'.
array<Type^>^temp1 = {String::typeid};
MethodBuilder^ myHelloMethod = myNestedClassType->DefineMethod( "HelloMethod", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Virtual), String::typeid, temp1 );
// Generate IL for 'GetGreeting' method.
ILGenerator^ myMethodIL = myHelloMethod->GetILGenerator();
myMethodIL->Emit( OpCodes::Ldstr, "Hi! " );
myMethodIL->Emit( OpCodes::Ldarg_1 );
array<Type^>^temp2 = {String::typeid,String::typeid};
MethodInfo^ infoMethod = String::typeid->GetMethod( "Concat", temp2 );
myMethodIL->Emit( OpCodes::Call, infoMethod );
myMethodIL->Emit( OpCodes::Ret );
MethodInfo^ myHelloMethodInfo = IMyInterface::typeid->GetMethod( "HelloMethod" );
// Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType->DefineMethodOverride( myHelloMethod, myHelloMethodInfo );
// Create 'MyHelloWorld' type.
Type^ myType = myHelloWorldType->CreateType();
// Create 'MyNestedClass' type.
return myNestedClassType->CreateType();
}
};
int main()
{
EmittedClass::Main();
}
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
public interface IMyInterface
{
String HelloMethod(String parameter);
}
public class Example
{
public static void Main()
{
Type myNestedClassType = CreateCallee(Thread.GetDomain());
// Cretae an instance of 'MyNestedClass'.
IMyInterface myInterface =
(IMyInterface)Activator.CreateInstance(myNestedClassType);
Console.WriteLine(myInterface.HelloMethod("Bill"));
}
// Create the callee transient dynamic assembly.
private static Type CreateCallee(AppDomain myAppDomain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "Example";
// Create the callee dynamic assembly.
AssemblyBuilder myAssembly =
myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
// Create a dynamic module in the callee assembly.
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
// Define a public class named "MyHelloWorld".
TypeBuilder myHelloWorldType =
myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
// Define a public nested class named 'MyNestedClass'.
TypeBuilder myNestedClassType =
myHelloWorldType.DefineNestedType("MyNestedClass",
TypeAttributes.NestedPublic, typeof(Example),
new Type[]{typeof(IMyInterface)});
// Implement 'IMyInterface' interface.
myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
// Define 'HelloMethod' of 'IMyInterface'.
MethodBuilder myHelloMethod =
myNestedClassType.DefineMethod("HelloMethod",
MethodAttributes.Public | MethodAttributes.Virtual,
typeof(String), new Type[]{typeof(String)});
// Generate IL for 'GetGreeting' method.
ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
myMethodIL.Emit(OpCodes.Ldarg_1);
MethodInfo infoMethod =
typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
myMethodIL.Emit(OpCodes.Call, infoMethod);
myMethodIL.Emit(OpCodes.Ret);
MethodInfo myHelloMethodInfo =
typeof(IMyInterface).GetMethod("HelloMethod");
// Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
// Create 'MyHelloWorld' type.
Type myType = myHelloWorldType.CreateType();
// Create 'MyNestedClass' type.
return myNestedClassType.CreateType();
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions
Public Interface IMyInterface
Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface
Public Class Example
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Shared Sub Main()
Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
' Create an instance of 'MyNestedClass'.
Dim myInterface As IMyInterface = _
CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
Console.WriteLine(myInterface.HelloMethod("Bill"))
End Sub
' Create the callee transient dynamic assembly.
Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "Example"
' Create the callee dynamic assembly.
Dim myAssembly As AssemblyBuilder = _
myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
' Create a dynamic module in the callee assembly.
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "MyHelloWorld".
Dim myHelloWorldType As TypeBuilder = _
myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
' Define a public nested class named 'MyNestedClass'.
Dim myNestedClassType As TypeBuilder = _
myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
GetType(Example), New Type() {GetType(IMyInterface)})
' Implement 'IMyInterface' interface.
myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
' Define 'HelloMethod' of 'IMyInterface'.
Dim myHelloMethod As MethodBuilder = _
myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
' Generate IL for 'GetGreeting' method.
Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
myMethodIL.Emit(OpCodes.Ldarg_1)
Dim infoMethod As MethodInfo = _
GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
myMethodIL.Emit(OpCodes.Call, infoMethod)
myMethodIL.Emit(OpCodes.Ret)
Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
' Implement 'HelloMethod' of 'IMyInterface'.
myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
' Create 'MyHelloWorld' type.
Dim myType As Type = myHelloWorldType.CreateType()
' Create 'MyNestedClass' type.
Return myNestedClassType.CreateType()
End Function 'CreateCallee
End Class