MethodBuilder 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义并表示动态类上的方法(或构造函数)。
public ref class MethodBuilder sealed : System::Reflection::MethodInfo
public ref class MethodBuilder abstract : System::Reflection::MethodInfo
public ref class MethodBuilder sealed : System::Reflection::MethodInfo, System::Runtime::InteropServices::_MethodBuilder
public sealed class MethodBuilder : System.Reflection.MethodInfo
public abstract class MethodBuilder : System.Reflection.MethodInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class MethodBuilder : System.Reflection.MethodInfo, System.Runtime.InteropServices._MethodBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class MethodBuilder : System.Reflection.MethodInfo, System.Runtime.InteropServices._MethodBuilder
type MethodBuilder = class
inherit MethodInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type MethodBuilder = class
inherit MethodInfo
interface _MethodBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MethodBuilder = class
inherit MethodInfo
interface _MethodBuilder
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
Public MustInherit Class MethodBuilder
Inherits MethodInfo
Public NotInheritable Class MethodBuilder
Inherits MethodInfo
Implements _MethodBuilder
- 继承
- 属性
- 实现
示例
以下示例使用 MethodBuilder 类在动态类型中创建方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
void AddMethodDynamically( TypeBuilder^ myTypeBld,
String^ mthdName,
array<Type^>^ mthdParams,
Type^ returnType,
String^ mthdAction )
{
MethodBuilder^ myMthdBld = myTypeBld->DefineMethod( mthdName, static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), returnType, mthdParams );
ILGenerator^ ILOut = myMthdBld->GetILGenerator();
int numParams = mthdParams->Length;
for ( Byte x = 0; x < numParams; x++ )
{
ILOut->Emit( OpCodes::Ldarg_S, x );
}
if ( numParams > 1 )
{
for ( int y = 0; y < (numParams - 1); y++ )
{
if ( mthdAction->Equals( "A" ) )
ILOut->Emit( OpCodes::Add );
else
if ( mthdAction->Equals( "M" ) )
ILOut->Emit( OpCodes::Mul );
else
ILOut->Emit( OpCodes::Add );
}
}
ILOut->Emit( OpCodes::Ret );
};
void main()
{
AppDomain^ myDomain = AppDomain::CurrentDomain;
AssemblyName^ asmName = gcnew AssemblyName;
asmName->Name = "MyDynamicAsm";
AssemblyBuilder^ myAsmBuilder = myDomain->DefineDynamicAssembly( asmName,
AssemblyBuilderAccess::RunAndSave );
ModuleBuilder^ myModule = myAsmBuilder->DefineDynamicModule( "MyDynamicAsm",
"MyDynamicAsm.dll" );
TypeBuilder^ myTypeBld = myModule->DefineType( "MyDynamicType",
TypeAttributes::Public );
// Get info from the user to build the method dynamically.
Console::WriteLine( "Let's build a simple method dynamically!" );
Console::WriteLine( "Please enter a few numbers, separated by spaces." );
String^ inputNums = Console::ReadLine();
Console::Write( "Do you want to [A]dd (default) or [M]ultiply these numbers? " );
String^ myMthdAction = Console::ReadLine()->ToUpper();
Console::Write( "Lastly, what do you want to name your new dynamic method? " );
String^ myMthdName = Console::ReadLine();
// Process inputNums into an array and create a corresponding Type array
int index = 0;
array<String^>^inputNumsList = inputNums->Split();
array<Type^>^myMthdParams = gcnew array<Type^>(inputNumsList->Length);
array<Object^>^inputValsList = gcnew array<Object^>(inputNumsList->Length);
for each (String^ inputNum in inputNumsList)
{
inputValsList[ index ] = Convert::ToInt32( inputNum );
myMthdParams[ index ] = int::typeid;
index++;
}
// Now, call the method building method with the parameters, passing the
// TypeBuilder by reference.
AddMethodDynamically( myTypeBld,
myMthdName,
myMthdParams,
int::typeid,
myMthdAction );
Type^ myType = myTypeBld->CreateType();
Console::WriteLine( "---" );
Console::WriteLine( "The result of {0} the inputted values is: {1}",
((myMthdAction->Equals( "M" )) ? "multiplying" : "adding"),
myType->InvokeMember( myMthdName,
BindingFlags::InvokeMethod | BindingFlags::Public | BindingFlags::Static,
nullptr,
nullptr,
inputValsList ) );
Console::WriteLine( "---" );
// Let's take a look at the method we created.
// If you are interested in seeing the MSIL generated dynamically for the method
// your program generated, change to the directory where you ran the compiled
// code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
// of manifest contents appears, click on "MyDynamicType" and then on the name of
// of the method you provided during execution.
myAsmBuilder->Save( "MyDynamicAsm.dll" );
MethodInfo^ myMthdInfo = myType->GetMethod( myMthdName );
Console::WriteLine( "Your Dynamic Method: {0};", myMthdInfo );
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DemoMethodBuilder
{
public static void AddMethodDynamically (TypeBuilder myTypeBld,
string mthdName,
Type[] mthdParams,
Type returnType,
string mthdAction)
{
MethodBuilder myMthdBld = myTypeBld.DefineMethod(
mthdName,
MethodAttributes.Public |
MethodAttributes.Static,
returnType,
mthdParams);
ILGenerator ILout = myMthdBld.GetILGenerator();
int numParams = mthdParams.Length;
for (byte x=0; x < numParams; x++)
{
ILout.Emit(OpCodes.Ldarg_S, x);
}
if (numParams > 1)
{
for (int y=0; y<(numParams-1); y++)
{
switch (mthdAction)
{
case "A": ILout.Emit(OpCodes.Add);
break;
case "M": ILout.Emit(OpCodes.Mul);
break;
default: ILout.Emit(OpCodes.Add);
break;
}
}
}
ILout.Emit(OpCodes.Ret);
}
public static void Main()
{
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName asmName = new AssemblyName();
asmName.Name = "MyDynamicAsm";
AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModule = myAsmBuilder.DefineDynamicModule("MyDynamicAsm",
"MyDynamicAsm.dll");
TypeBuilder myTypeBld = myModule.DefineType("MyDynamicType",
TypeAttributes.Public);
// Get info from the user to build the method dynamically.
Console.WriteLine("Let's build a simple method dynamically!");
Console.WriteLine("Please enter a few numbers, separated by spaces.");
string inputNums = Console.ReadLine();
Console.Write("Do you want to [A]dd (default) or [M]ultiply these numbers? ");
string myMthdAction = Console.ReadLine().ToUpper();
Console.Write("Lastly, what do you want to name your new dynamic method? ");
string myMthdName = Console.ReadLine();
// Process inputNums into an array and create a corresponding Type array
int index = 0;
string[] inputNumsList = inputNums.Split();
Type[] myMthdParams = new Type[inputNumsList.Length];
object[] inputValsList = new object[inputNumsList.Length];
foreach (string inputNum in inputNumsList)
{
inputValsList[index] = (object)Convert.ToInt32(inputNum);
myMthdParams[index] = typeof(int);
index++;
}
// Now, call the method building method with the parameters, passing the
// TypeBuilder by reference.
AddMethodDynamically(myTypeBld,
myMthdName,
myMthdParams,
typeof(int),
myMthdAction);
Type myType = myTypeBld.CreateType();
Console.WriteLine("---");
Console.WriteLine("The result of {0} the inputted values is: {1}",
((myMthdAction == "M") ? "multiplying" : "adding"),
myType.InvokeMember(myMthdName,
BindingFlags.InvokeMethod | BindingFlags.Public |
BindingFlags.Static,
null,
null,
inputValsList));
Console.WriteLine("---");
// Let's take a look at the method we created.
// If you are interested in seeing the MSIL generated dynamically for the method
// your program generated, change to the directory where you ran the compiled
// code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
// of manifest contents appears, click on "MyDynamicType" and then on the name of
// of the method you provided during execution.
myAsmBuilder.Save("MyDynamicAsm.dll");
MethodInfo myMthdInfo = myType.GetMethod(myMthdName);
Console.WriteLine("Your Dynamic Method: {0};", myMthdInfo.ToString());
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Class DemoMethodBuilder
Public Shared Sub AddMethodDynamically(ByVal myTypeBld As TypeBuilder, _
ByVal mthdName As String, _
ByVal mthdParams() As Type, _
ByVal returnType As Type, _
ByVal mthdAction As String)
Dim myMthdBld As MethodBuilder = myTypeBld.DefineMethod(mthdName, _
MethodAttributes.Public Or MethodAttributes.Static, _
returnType, _
mthdParams)
Dim ILout As ILGenerator = myMthdBld.GetILGenerator()
Dim numParams As Integer = mthdParams.Length
Dim x As Byte
For x = 0 To numParams - 1
ILout.Emit(OpCodes.Ldarg_S, x)
Next x
If numParams > 1 Then
Dim y As Integer
For y = 0 To (numParams - 1) - 1
Select Case mthdAction
Case "A"
ILout.Emit(OpCodes.Add)
Case "M"
ILout.Emit(OpCodes.Mul)
Case Else
ILout.Emit(OpCodes.Add)
End Select
Next y
End If
ILout.Emit(OpCodes.Ret)
End Sub
Public Shared Sub Main()
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim asmName As New AssemblyName()
asmName.Name = "MyDynamicAsm"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(asmName, _
AssemblyBuilderAccess.RunAndSave)
Dim myModule As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyDynamicAsm", _
"MyDynamicAsm.dll")
Dim myTypeBld As TypeBuilder = myModule.DefineType("MyDynamicType", TypeAttributes.Public)
' Get info from the user to build the method dynamically.
Console.WriteLine("Let's build a simple method dynamically!")
Console.WriteLine("Please enter a few numbers, separated by spaces.")
Dim inputNums As String = Console.ReadLine()
Console.Write("Do you want to [A]dd (default) or [M]ultiply these numbers? ")
Dim myMthdAction As String = Console.ReadLine().ToUpper()
Console.Write("Lastly, what do you want to name your new dynamic method? ")
Dim myMthdName As String = Console.ReadLine()
' Process inputNums into an array and create a corresponding Type array
Dim index As Integer = 0
Dim inputNumsList As String() = inputNums.Split()
Dim myMthdParams(inputNumsList.Length - 1) As Type
Dim inputValsList(inputNumsList.Length - 1) As Object
Dim inputNum As String
For Each inputNum In inputNumsList
inputValsList(index) = CType(Convert.ToInt32(inputNum), Object)
myMthdParams(index) = GetType(Integer)
index += 1
Next inputNum
' Now, call the method building method with the parameters, passing the
' TypeBuilder by reference.
AddMethodDynamically(myTypeBld, myMthdName, myMthdParams, GetType(Integer), myMthdAction)
Dim myType As Type = myTypeBld.CreateType()
Dim description as String
If myMthdAction = "M" Then
description = "multiplying"
Else
description = "adding"
End If
Console.WriteLine("---")
Console.WriteLine("The result of {0} the values is: {1}", _
description, _
myType.InvokeMember(myMthdName, _
BindingFlags.InvokeMethod _
Or BindingFlags.Public _
Or BindingFlags.Static, _
Nothing, _
Nothing, _
inputValsList))
Console.WriteLine("---")
' If you are interested in seeing the MSIL generated dynamically for the method
' your program generated, change to the directory where you ran the compiled
' code sample and type "ildasm MyDynamicAsm.dll" at the prompt. When the list
' of manifest contents appears, click on "MyDynamicType" and then on the name of
' of the method you provided during execution.
myAsmBuilder.Save("MyDynamicAsm.dll")
Dim myMthdInfo As MethodInfo = myType.GetMethod(myMthdName)
Console.WriteLine("Your Dynamic Method: {0};", myMthdInfo.ToString())
End Sub
End Class
注解
有关此 API 的详细信息,请参阅 MethodBuilder 的补充 API 说明。
构造函数
MethodBuilder() |
初始化 MethodBuilder 类的新实例。 |
属性
Attributes |
检索此方法的属性。 |
CallingConvention |
返回此方法的调用约定。 |
ContainsGenericParameters |
不支持此类型。 |
ContainsGenericParameters |
获取一个值,该值指示泛型方法是否包含未分配的泛型类型参数。 (继承自 MethodInfo) |
CustomAttributes |
获取包含此成员自定义属性的集合。 (继承自 MemberInfo) |
DeclaringType |
返回声明此方法的类型。 |
InitLocals |
获取或设置一个布尔值,该值指定此方法中的局部变量是否初始化为零。 此属性的默认值为 |
InitLocalsCore |
在派生类中重写时,获取或设置一个值,该值指示此方法中的局部变量是否为零初始化。 |
IsAbstract |
获取一个值,该值指示此方法是否为抽象方法。 (继承自 MethodBase) |
IsAssembly |
获取一个值,该值指示此方法或构造函数的潜在可见性是否由 Assembly 描述;也就是说,此方法或构造函数只对同一程序集中的其他类型可见,而对该程序集以外的派生类型则不可见。 (继承自 MethodBase) |
IsCollectible |
获取一个值,该值指示此 MemberInfo 对象是否是包含在可回收的 AssemblyLoadContext 中的程序集的一部分。 (继承自 MemberInfo) |
IsConstructedGenericMethod |
定义并表示动态类上的方法(或构造函数)。 |
IsConstructedGenericMethod |
定义并表示动态类上的方法(或构造函数)。 (继承自 MethodBase) |
IsConstructor |
获取一个值,该值指示此方法是否为构造函数。 (继承自 MethodBase) |
IsFamily |
获取一个值,该值指示此方法或构造函数的可见性是否由 Family 描述;也就是说,此方法或构造函数仅在其类和派生类内可见。 (继承自 MethodBase) |
IsFamilyAndAssembly |
获取一个值,该值指示此方法或构造函数的可见性是否由 FamANDAssem 描述;也就是说,此方法或构造函数可由派生类调用,但仅当这些派生类在同一程序集中时。 (继承自 MethodBase) |
IsFamilyOrAssembly |
获取一个值,该值指示此方法或构造函数的潜在可见性是否由 FamORAssem 描述;也就是说,此方法或构造函数可由派生类(无论其位置如何)和同一程序集中的类调用。 (继承自 MethodBase) |
IsFinal |
获取一个值,该值指示此方法是否为 |
IsGenericMethod |
获取一个值,该值指示方法是否为泛型方法。 |
IsGenericMethod |
获取一个值,该值指示当前方法是否为泛型方法。 (继承自 MethodInfo) |
IsGenericMethodDefinition |
获取一个值,该值指示当前 MethodBuilder 对象是否表示泛型方法的定义。 |
IsGenericMethodDefinition |
获取一个值,该值指示当前 MethodInfo 是否表示泛型方法的定义。 (继承自 MethodInfo) |
IsHideBySig |
获取一个值,该值指示是否只有一个签名完全相同的同一种类的成员在派生类中是隐藏的。 (继承自 MethodBase) |
IsPrivate |
获取一个值,该值指示此成员是否是私有的。 (继承自 MethodBase) |
IsPublic |
获取一个值,该值指示这是否是一个公共方法。 (继承自 MethodBase) |
IsSecurityCritical |
在所有情况下均引发 NotSupportedException。 |
IsSecurityCritical |
获取一个值,该值指示当前方法或构造函数在当前信任级别上是安全关键的还是安全可靠关键的,因此可以执行关键操作。 (继承自 MethodBase) |
IsSecuritySafeCritical |
在所有情况下均引发 NotSupportedException。 |
IsSecuritySafeCritical |
获取一个值,该值指示当前方法或构造函数在当前信任级别上是安全可靠关键的;即它是否可以执行关键操作并可以由透明代码访问。 (继承自 MethodBase) |
IsSecurityTransparent |
在所有情况下均引发 NotSupportedException。 |
IsSecurityTransparent |
获取一个值,该值指示当前方法或构造函数在当前信任级别上是透明的,因此无法执行关键操作。 (继承自 MethodBase) |
IsSpecialName |
获取一个值,该值指示此方法是否具有特殊名称。 (继承自 MethodBase) |
IsStatic |
获取一个值,该值指示方法是否为 |
IsVirtual |
获取一个值,该值指示方法是否为 |
MemberType |
获取一个 MemberTypes 值,该值指示此成员是方法。 (继承自 MethodInfo) |
MetadataToken |
获取一个标记,该标记用于标识元数据中的当前动态模块。 |
MetadataToken |
获取一个值,该值标识元数据元素。 (继承自 MemberInfo) |
MethodHandle |
检索此方法的内部句柄。 使用此句柄来访问基础元数据句柄。 |
MethodHandle |
获取方法的内部元数据表示形式的句柄。 (继承自 MethodBase) |
MethodImplementationFlags |
定义并表示动态类上的方法(或构造函数)。 |
MethodImplementationFlags |
获取指定方法实现特性的 MethodImplAttributes 标志。 (继承自 MethodBase) |
Module |
获取在其中定义了当前方法的模块。 |
Module |
获取一个模块,在该模块中已经定义一个类型,该类型用于声明由当前 MemberInfo 表示的成员。 (继承自 MemberInfo) |
Name |
检索此方法的名称。 |
ReflectedType |
检索在反射中用于获取此对象的类。 |
ReflectedType |
获取用于获取 |
ReturnParameter |
获取一个 ParameterInfo 对象,该对象包含有关方法的返回类型的信息(例如返回类型是否具有自定义修饰符)。 |
ReturnParameter |
获取一个 ParameterInfo 对象,该对象包含有关方法的返回类型的信息(例如返回类型是否具有自定义修饰符)。 (继承自 MethodInfo) |
ReturnType |
获取此 MethodBuilder 所表示的方法的返回类型。 |
ReturnType |
获取此方法的返回类型。 (继承自 MethodInfo) |
ReturnTypeCustomAttributes |
返回此方法的返回类型的自定义属性。 |
ReturnTypeCustomAttributes |
获取返回类型的自定义属性。 (继承自 MethodInfo) |
Signature |
检索方法的签名。 |