DynamicMethod 类

定义

定义并表示可以编译、执行和丢弃的一种动态方法。 丢弃的方法可用于垃圾回收。

public ref class DynamicMethod sealed : System::Reflection::MethodInfo
public sealed class DynamicMethod : System.Reflection.MethodInfo
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DynamicMethod : System.Reflection.MethodInfo
type DynamicMethod = class
    inherit MethodInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
type DynamicMethod = class
    inherit MethodInfo
Public NotInheritable Class DynamicMethod
Inherits MethodInfo
继承
属性

示例

下面的代码示例创建一个采用两个参数的动态方法。 该示例发出一个简单的函数体,该函数体将第一个参数打印到控制台,该示例使用第二个参数作为 方法的返回值。 该示例通过创建委托完成方法,使用不同的参数调用委托,最后使用 Invoke 方法调用动态方法。

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Globalization;

// Declare a delegate type that can be used to execute the completed
// dynamic method. 
private delegate int HelloDelegate(String^ msg, int ret);

void main()
{
    // Create an array that specifies the types of the parameters
    // of the dynamic method. This dynamic method has a String
    // parameter and an Integer parameter.
    array<Type^>^ helloArgs = { String::typeid, int::typeid };

    // Create a dynamic method with the name "Hello", a return type
    // of Integer, and two parameters whose types are specified by
    // the array helloArgs. Create the method in the module that
    // defines the String class.
    DynamicMethod^ hello = gcnew DynamicMethod("Hello", 
        int::typeid, 
        helloArgs, 
        String::typeid->Module);

    // Create an array that specifies the parameter types of the
    // overload of Console::WriteLine to be used in Hello.
    array<Type^>^ writeStringArgs = { String::typeid };
    // Get the overload of Console::WriteLine that has one
    // String parameter.
    MethodInfo^ writeString = Console::typeid->GetMethod("WriteLine", 
        writeStringArgs);

    // Get an ILGenerator and emit a body for the dynamic method,
    // using a stream size larger than the IL that will be
    // emitted.
    ILGenerator^ il = hello->GetILGenerator(256);
    // Load the first argument, which is a string, onto the stack.
    il->Emit(OpCodes::Ldarg_0);
    // Call the overload of Console::WriteLine that prints a string.
    il->EmitCall(OpCodes::Call, writeString, nullptr);
    // The Hello method returns the value of the second argument;
    // to do this, load the onto the stack and return.
    il->Emit(OpCodes::Ldarg_1);
    il->Emit(OpCodes::Ret);

    // Add parameter information to the dynamic method. (This is not
    // necessary, but can be useful for debugging.) For each parameter,
    // identified by position, supply the parameter attributes and a 
    // parameter name.
    hello->DefineParameter(1, ParameterAttributes::In, "message");
    hello->DefineParameter(2, ParameterAttributes::In, "valueToReturn");

    // Create a delegate that represents the dynamic method. This
    // action completes the method. Any further attempts to
    // change the method are ignored.
    HelloDelegate^ hi = 
        (HelloDelegate^) hello->CreateDelegate(HelloDelegate::typeid);

    // Use the delegate to execute the dynamic method.
    Console::WriteLine("\r\nUse the delegate to execute the dynamic method:");
    int retval = hi("\r\nHello, World!", 42);
    Console::WriteLine("Invoking delegate hi(\"Hello, World!\", 42) returned: " + retval);

    // Execute it again, with different arguments.
    retval = hi("\r\nHi, Mom!", 5280);
    Console::WriteLine("Invoking delegate hi(\"Hi, Mom!\", 5280) returned: " + retval);

    Console::WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
    // Create an array of arguments to use with the Invoke method.
    array<Object^>^ invokeArgs = { "\r\nHello, World!", 42 };
    // Invoke the dynamic method using the arguments. This is much
    // slower than using the delegate, because you must create an
    // array to contain the arguments, and value-type arguments
    // must be boxed.
    Object^ objRet = hello->Invoke(nullptr, BindingFlags::ExactBinding, nullptr, invokeArgs, gcnew CultureInfo("en-us"));
    Console::WriteLine("hello.Invoke returned: " + objRet);

    Console::WriteLine("\r\n ----- Display information about the dynamic method -----");
    // Display MethodAttributes for the dynamic method, set when 
    // the dynamic method was created.
    Console::WriteLine("\r\nMethod Attributes: {0}", hello->Attributes);

    // Display the calling convention of the dynamic method, set when the 
    // dynamic method was created.
    Console::WriteLine("\r\nCalling convention: {0}", hello->CallingConvention);

    // Display the declaring type, which is always null for dynamic
    // methods.
    if (hello->DeclaringType == nullptr)
    {
        Console::WriteLine("\r\nDeclaringType is always null for dynamic methods.");
    }
    else
    {
        Console::WriteLine("DeclaringType: {0}", hello->DeclaringType);
    }

    // Display the default value for InitLocals.
    if (hello->InitLocals)
    {
        Console::Write("\r\nThis method contains verifiable code.");
    }
    else
    {
        Console::Write("\r\nThis method contains unverifiable code.");
    }
    Console::WriteLine(" (InitLocals = {0})", hello->InitLocals);

    // Display the module specified when the dynamic method was created.
    Console::WriteLine("\r\nModule: {0}", hello->Module);

    // Display the name specified when the dynamic method was created.
    // Note that the name can be blank.
    Console::WriteLine("\r\nName: {0}", hello->Name);

    // For dynamic methods, the reflected type is always null.
    if (hello->ReflectedType == nullptr)
    {
        Console::WriteLine("\r\nReflectedType is null.");
    }
    else
    {
        Console::WriteLine("\r\nReflectedType: {0}", hello->ReflectedType);
    }

    if (hello->ReturnParameter == nullptr)
    {
        Console::WriteLine("\r\nMethod has no return parameter.");
    }
    else
    {
        Console::WriteLine("\r\nReturn parameter: {0}", hello->ReturnParameter);
    }

    // If the method has no return type, ReturnType is System.Void.
    Console::WriteLine("\r\nReturn type: {0}", hello->ReturnType);

    // ReturnTypeCustomAttributes returns an ICustomeAttributeProvider
    // that can be used to enumerate the custom attributes of the
    // return value. At present, there is no way to set such custom
    // attributes, so the list is empty.
    if (hello->ReturnType == Void::typeid)
    {
        Console::WriteLine("The method has no return type.");
    }
    else
    {
        ICustomAttributeProvider^ caProvider = hello->ReturnTypeCustomAttributes;
        array<Object^>^ returnAttributes = caProvider->GetCustomAttributes(true);
        if (returnAttributes->Length == 0)
        {
            Console::WriteLine("\r\nThe return type has no custom attributes.");
        }
        else
        {
            Console::WriteLine("\r\nThe return type has the following custom attributes:");
            for each (Object^ attr in returnAttributes)
            {
                Console::WriteLine("\t{0}", attr->ToString());
            }
        }
    }

    Console::WriteLine("\r\nToString: {0}", hello->ToString());

    // Display parameter information.
    array<ParameterInfo^>^ parameters = hello->GetParameters();
    Console::WriteLine("\r\nParameters: name, type, ParameterAttributes");
    for each (ParameterInfo^ p in parameters)
    {
        Console::WriteLine("\t{0}, {1}, {2}", 
            p->Name, p->ParameterType, p->Attributes);
    }
}

/* This code example produces the following output:

Use the delegate to execute the dynamic method:

Hello, World!
Invoking delegate hi("Hello, World!", 42) returned: 42

Hi, Mom!
Invoking delegate hi("Hi, Mom!", 5280) returned: 5280

Use the Invoke method to execute the dynamic method:

Hello, World!
hello.Invoke returned: 42

 ----- Display information about the dynamic method -----

Method Attributes: PrivateScope, Public, Static

Calling convention: Standard

DeclaringType is always null for dynamic methods.

This method contains verifiable code. (InitLocals = True)

Module: CommonLanguageRuntimeLibrary

Name: Hello

ReflectedType is null.

Method has no return parameter.

Return type: System.Int32

The return type has no custom attributes.

ToString: Int32 Hello(System.String, Int32)

Parameters: name, type, ParameterAttributes
        message, System.String, In
        valueToReturn, System.Int32, In
 */
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Globalization;

public class Test
{
    // Declare a delegate type that can be used to execute the completed
    // dynamic method.
    private delegate int HelloDelegate(string msg, int ret);

    public static void Main()
    {
        // Create an array that specifies the types of the parameters
        // of the dynamic method. This dynamic method has a String
        // parameter and an Integer parameter.
        Type[] helloArgs = {typeof(string), typeof(int)};

        // Create a dynamic method with the name "Hello", a return type
        // of Integer, and two parameters whose types are specified by
        // the array helloArgs. Create the method in the module that
        // defines the String class.
        DynamicMethod hello = new DynamicMethod("Hello",
            typeof(int),
            helloArgs,
            typeof(string).Module);

        // Create an array that specifies the parameter types of the
        // overload of Console.WriteLine to be used in Hello.
        Type[] writeStringArgs = {typeof(string)};
        // Get the overload of Console.WriteLine that has one
        // String parameter.
        MethodInfo writeString = typeof(Console).GetMethod("WriteLine",
            writeStringArgs);

        // Get an ILGenerator and emit a body for the dynamic method,
        // using a stream size larger than the IL that will be
        // emitted.
        ILGenerator il = hello.GetILGenerator(256);
        // Load the first argument, which is a string, onto the stack.
        il.Emit(OpCodes.Ldarg_0);
        // Call the overload of Console.WriteLine that prints a string.
        il.EmitCall(OpCodes.Call, writeString, null);
        // The Hello method returns the value of the second argument;
        // to do this, load the onto the stack and return.
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Ret);

        // Add parameter information to the dynamic method. (This is not
        // necessary, but can be useful for debugging.) For each parameter,
        // identified by position, supply the parameter attributes and a
        // parameter name.
        hello.DefineParameter(1, ParameterAttributes.In, "message");
        hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn");

        // Create a delegate that represents the dynamic method. This
        // action completes the method. Any further attempts to
        // change the method are ignored.
        HelloDelegate hi =
            (HelloDelegate) hello.CreateDelegate(typeof(HelloDelegate));

        // Use the delegate to execute the dynamic method.
        Console.WriteLine("\r\nUse the delegate to execute the dynamic method:");
        int retval = hi("\r\nHello, World!", 42);
        Console.WriteLine("Invoking delegate hi(\"Hello, World!\", 42) returned: " + retval);

        // Execute it again, with different arguments.
        retval = hi("\r\nHi, Mom!", 5280);
        Console.WriteLine("Invoking delegate hi(\"Hi, Mom!\", 5280) returned: " + retval);

        Console.WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
        // Create an array of arguments to use with the Invoke method.
        object[] invokeArgs = {"\r\nHello, World!", 42};
        // Invoke the dynamic method using the arguments. This is much
        // slower than using the delegate, because you must create an
        // array to contain the arguments, and value-type arguments
        // must be boxed.
        object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us"));
        Console.WriteLine("hello.Invoke returned: " + objRet);

        Console.WriteLine("\r\n ----- Display information about the dynamic method -----");
        // Display MethodAttributes for the dynamic method, set when
        // the dynamic method was created.
        Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes);

        // Display the calling convention of the dynamic method, set when the
        // dynamic method was created.
        Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention);

        // Display the declaring type, which is always null for dynamic
        // methods.
        if (hello.DeclaringType == null)
        {
            Console.WriteLine("\r\nDeclaringType is always null for dynamic methods.");
        }
        else
        {
            Console.WriteLine("DeclaringType: {0}", hello.DeclaringType);
        }

        // Display the default value for InitLocals.
        if (hello.InitLocals)
        {
            Console.Write("\r\nThis method contains verifiable code.");
        }
        else
        {
            Console.Write("\r\nThis method contains unverifiable code.");
        }
        Console.WriteLine(" (InitLocals = {0})", hello.InitLocals);

        // Display the module specified when the dynamic method was created.
        Console.WriteLine("\r\nModule: {0}", hello.Module);

        // Display the name specified when the dynamic method was created.
        // Note that the name can be blank.
        Console.WriteLine("\r\nName: {0}", hello.Name);

        // For dynamic methods, the reflected type is always null.
        if (hello.ReflectedType == null)
        {
            Console.WriteLine("\r\nReflectedType is null.");
        }
        else
        {
            Console.WriteLine("\r\nReflectedType: {0}", hello.ReflectedType);
        }

        if (hello.ReturnParameter == null)
        {
            Console.WriteLine("\r\nMethod has no return parameter.");
        }
        else
        {
            Console.WriteLine("\r\nReturn parameter: {0}", hello.ReturnParameter);
        }

        // If the method has no return type, ReturnType is System.Void.
        Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType);

        // ReturnTypeCustomAttributes returns an ICustomeAttributeProvider
        // that can be used to enumerate the custom attributes of the
        // return value. At present, there is no way to set such custom
        // attributes, so the list is empty.
        if (hello.ReturnType == typeof(void))
        {
            Console.WriteLine("The method has no return type.");
        }
        else
        {
            ICustomAttributeProvider caProvider = hello.ReturnTypeCustomAttributes;
            object[] returnAttributes = caProvider.GetCustomAttributes(true);
            if (returnAttributes.Length == 0)
            {
                Console.WriteLine("\r\nThe return type has no custom attributes.");
            }
            else
            {
                Console.WriteLine("\r\nThe return type has the following custom attributes:");
                foreach( object attr in returnAttributes )
                {
                    Console.WriteLine("\t{0}", attr.ToString());
                }
            }
        }

        Console.WriteLine("\r\nToString: {0}", hello.ToString());

        // Display parameter information.
        ParameterInfo[] parameters = hello.GetParameters();
        Console.WriteLine("\r\nParameters: name, type, ParameterAttributes");
        foreach( ParameterInfo p in parameters )
        {
            Console.WriteLine("\t{0}, {1}, {2}",
                p.Name, p.ParameterType, p.Attributes);
        }
    }
}

/* This code example produces the following output:

Use the delegate to execute the dynamic method:

Hello, World!
Invoking delegate hi("Hello, World!", 42) returned: 42

Hi, Mom!
Invoking delegate hi("Hi, Mom!", 5280) returned: 5280

Use the Invoke method to execute the dynamic method:

Hello, World!
hello.Invoke returned: 42

 ----- Display information about the dynamic method -----

Method Attributes: PrivateScope, Public, Static

Calling convention: Standard

DeclaringType is always null for dynamic methods.

This method contains verifiable code. (InitLocals = True)

Module: CommonLanguageRuntimeLibrary

Name: Hello

ReflectedType is null.

Method has no return parameter.

Return type: System.Int32

The return type has no custom attributes.

ToString: Int32 Hello(System.String, Int32)

Parameters: name, type, ParameterAttributes
        message, System.String, In
        valueToReturn, System.Int32, In
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Globalization

Public Class Test
    ' Declare a delegate type that can be used to execute the completed
    ' dynamic method. 
    Private Delegate Function HelloDelegate(ByVal msg As String, _
        ByVal ret As Integer) As Integer

    Public Shared Sub Main()
        ' Create an array that specifies the types of the parameters
        ' of the dynamic method. This dynamic method has a String
        ' parameter and an Integer parameter.
        Dim helloArgs() As Type = {GetType(String), GetType(Integer)}

        ' Create a dynamic method with the name "Hello", a return type
        ' of Integer, and two parameters whose types are specified by
        ' the array helloArgs. Create the method in the module that
        ' defines the String class.
        Dim hello As New DynamicMethod("Hello", _
            GetType(Integer), _
            helloArgs, _
            GetType(String).Module)

        ' Create an array that specifies the parameter types of the
        ' overload of Console.WriteLine to be used in Hello.
        Dim writeStringArgs() As Type = {GetType(String)}
        ' Get the overload of Console.WriteLine that has one
        ' String parameter.
        Dim writeString As MethodInfo = GetType(Console). _
            GetMethod("WriteLine", writeStringArgs) 

        ' Get an ILGenerator and emit a body for the dynamic method,
        ' using a stream size larger than the IL that will be
        ' emitted.
        Dim il As ILGenerator = hello.GetILGenerator(256)
        ' Load the first argument, which is a string, onto the stack.
        il.Emit(OpCodes.Ldarg_0)
        ' Call the overload of Console.WriteLine that prints a string.
        il.EmitCall(OpCodes.Call, writeString, Nothing)
        ' The Hello method returns the value of the second argument;
        ' to do this, load the onto the stack and return.
        il.Emit(OpCodes.Ldarg_1)
        il.Emit(OpCodes.Ret)

        ' Add parameter information to the dynamic method. (This is not
        ' necessary, but can be useful for debugging.) For each parameter,
        ' identified by position, supply the parameter attributes and a 
        ' parameter name.
        hello.DefineParameter(1, ParameterAttributes.In, "message")
        hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn")

        ' Create a delegate that represents the dynamic method. This
        ' action completes the method. Any further attempts to
        ' change the method are ignored.
    Dim hi As HelloDelegate = _
            CType(hello.CreateDelegate(GetType(HelloDelegate)), HelloDelegate)

        ' Use the delegate to execute the dynamic method.
        Console.WriteLine(vbCrLf & "Use the delegate to execute the dynamic method:")
        Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
        Console.WriteLine("Invoking delegate hi(""Hello, World!"", 42) returned: " _
            & retval & ".")

        ' Execute it again, with different arguments.
        retval = hi(vbCrLf & "Hi, Mom!", 5280)
        Console.WriteLine("Invoking delegate hi(""Hi, Mom!"", 5280) returned: " _
            & retval & ".")

        Console.WriteLine(vbCrLf & "Use the Invoke method to execute the dynamic method:")
        ' Create an array of arguments to use with the Invoke method.
        Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
        ' Invoke the dynamic method using the arguments. This is much
        ' slower than using the delegate, because you must create an
        ' array to contain the arguments, and value-type arguments
        ' must be boxed.
        Dim objRet As Object = hello.Invoke(Nothing, _
            BindingFlags.ExactBinding, Nothing, invokeArgs, _
            New CultureInfo("en-us"))
        Console.WriteLine("hello.Invoke returned: {0}", objRet)

        Console.WriteLine(vbCrLf & _
            " ----- Display information about the dynamic method -----")
        ' Display MethodAttributes for the dynamic method, set when 
        ' the dynamic method was created.
        Console.WriteLine(vbCrLf & "Method Attributes: {0}", _
            hello.Attributes)

        ' Display the calling convention of the dynamic method, set when the 
        ' dynamic method was created.
        Console.WriteLine(vbCrLf & "Calling convention: {0}", _ 
            hello.CallingConvention)

        ' Display the declaring type, which is always Nothing for dynamic
        ' methods.
        If hello.DeclaringType Is Nothing Then
            Console.WriteLine(vbCrLf & "DeclaringType is always Nothing for dynamic methods.")
        Else
            Console.WriteLine("DeclaringType: {0}", hello.DeclaringType)
        End If

        ' Display the default value for InitLocals.
        If hello.InitLocals Then
            Console.Write(vbCrLf & "This method contains verifiable code.")
        Else
            Console.Write(vbCrLf & "This method contains unverifiable code.")
        End If
        Console.WriteLine(" (InitLocals = {0})", hello.InitLocals)

        ' Display the module specified when the dynamic method was created.
        Console.WriteLine(vbCrLf & "Module: {0}", hello.Module)

        ' Display the name specified when the dynamic method was created.
        ' Note that the name can be blank.
        Console.WriteLine(vbCrLf & "Name: {0}", hello.Name)

        ' For dynamic methods, the reflected type is always Nothing.
        If hello.ReflectedType Is Nothing Then
            Console.WriteLine(vbCrLf & "ReflectedType is Nothing.")
        Else
            Console.WriteLine(vbCrLf & "ReflectedType: {0}", _
                hello.ReflectedType)
        End If

        If hello.ReturnParameter Is Nothing Then
            Console.WriteLine(vbCrLf & "Method has no return parameter.")
        Else
            Console.WriteLine(vbCrLf & "Return parameter: {0}", _
                hello.ReturnParameter)
        End If

        ' If the method has no return type, ReturnType is System.Void.
        Console.WriteLine(vbCrLf & "Return type: {0}", hello.ReturnType)           

        ' ReturnTypeCustomAttributes returns an ICustomeAttributeProvider
        ' that can be used to enumerate the custom attributes of the
        ' return value. At present, there is no way to set such custom
        ' attributes, so the list is empty.
        If hello.ReturnType Is GetType(System.Void) Then
            Console.WriteLine("The method has no return type.")
        Else
            Dim caProvider As ICustomAttributeProvider = _
                hello.ReturnTypeCustomAttributes
            Dim returnAttributes() As Object = _
                caProvider.GetCustomAttributes(True)
            If returnAttributes.Length = 0 Then
                Console.WriteLine(vbCrLf _
                    & "The return type has no custom attributes.")
            Else
                Console.WriteLine(vbCrLf _
                    & "The return type has the following custom attributes:")
                For Each attr As Object In returnAttributes
                    Console.WriteLine(vbTab & attr.ToString())
                Next attr
            End If
        End If

        Console.WriteLine(vbCrLf & "ToString: " & hello.ToString())

        ' Display parameter information.
        Dim parameters() As ParameterInfo = hello.GetParameters()
        Console.WriteLine(vbCrLf & "Parameters: name, type, ParameterAttributes")
        For Each p As ParameterInfo In parameters
            Console.WriteLine(vbTab & "{0}, {1}, {2}", _ 
                p.Name, p.ParameterType, p.Attributes)
        Next p
    End Sub
End Class

' This code example produces the following output:
'
'Use the delegate to execute the dynamic method:
'
'Hello, World!
'Invoking delegate hi("Hello, World!", 42) returned: 42.
'
'Hi, Mom!
'Invoking delegate hi("Hi, Mom!", 5280) returned: 5280.
'
'Use the Invoke method to execute the dynamic method:
'
'Hello, World!
'hello.Invoke returned: 42
'
' ----- Display information about the dynamic method -----
'
'Method Attributes: PrivateScope, Public, Static
'
'Calling convention: Standard
'
'DeclaringType is always Nothing for dynamic methods.
'
'This method contains verifiable code. (InitLocals = True)
'
'Module: CommonLanguageRuntimeLibrary
'
'Name: Hello
'
'ReflectedType is Nothing.
'
'Method has no return parameter.
'
'Return type: System.Int32
'
'The return type has no custom attributes.
'
'ToString: Int32 Hello(System.String, Int32)
'
'Parameters: name, type, ParameterAttributes
'        message, System.String, In
'        valueToReturn, System.Int32, In

注解

有关此 API 的详细信息,请参阅 DynamicMethod 的补充 API 备注

构造函数

DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Module, Boolean)

创建一个对模块全局有效的动态方法,指定方法名称、属性、调用约定、返回类型、参数类型和模块,并指定动态方法的 Microsoft 中间语言 (MSIL) 访问的类型和成员是否应跳过实时 (JIT) 可见性检查。

DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type, Boolean)

创建一个动态方法,指定方法名称、属性、调用约定、返回类型、参数类型,以及与该动态方法逻辑上相关联的类型,并指定动态方法的 Microsoft 中间语言 (MSIL) 访问的类型和成员是否应跳过实时 (JIT) 可见性检查。

DynamicMethod(String, Type, Type[])

初始化匿名托管的动态方法,指定方法名称、返回类型和参数类型。

DynamicMethod(String, Type, Type[], Boolean)

初始化匿名托管的动态方法,同时指定方法名称、返回类型、参数类型并指定是否应针对动态方法的 Microsoft 中间语言 (MSIL) 访问的类型和成员跳过实时 (JIT) 可见性检查。

DynamicMethod(String, Type, Type[], Module)

创建一个对模块全局有效的动态方法,指定方法名称、返回类型、参数类型和模块。

DynamicMethod(String, Type, Type[], Module, Boolean)

创建一个对模块全局有效的动态方法,指定方法名称、返回类型、参数类型和模块,并指定动态方法的 Microsoft 中间语言 (MSIL) 访问的类型和成员是否应跳过实时 (JIT) 可见性检查。

DynamicMethod(String, Type, Type[], Type)

创建动态方法,并指定方法的名称、返回类型、参数类型和此动态方法与之在逻辑上相关联的类型。

DynamicMethod(String, Type, Type[], Type, Boolean)

创建一个动态方法,指定方法名称、返回类型、参数类型,以及与该动态方法逻辑上相关联的类型,并指定动态方法的 Microsoft 中间语言 (MSIL) 访问的类型和成员是否应跳过实时 (JIT) 可见性检查。

属性

Attributes

创建动态方法后获取指定的属性。

CallingConvention

创建动态方法后获取指定的调用约定。

ContainsGenericParameters

获取一个值,该值指示泛型方法是否包含未分配的泛型类型参数。

(继承自 MethodInfo)
CustomAttributes

获取包含此成员自定义属性的集合。

(继承自 MemberInfo)
DeclaringType

获取声明方法的类型,对于动态方法,此类型始终为 null

InitLocals

获取或设置一个值,该值指示方法中的本地变量是否初始化为零。

IsAbstract

获取一个值,该值指示此方法是否为抽象方法。

(继承自 MethodBase)
IsAssembly

获取一个值,该值指示此方法或构造函数的潜在可见性是否由 Assembly 描述;也就是说,此方法或构造函数只对同一程序集中的其他类型可见,而对该程序集以外的派生类型则不可见。

(继承自 MethodBase)
IsCollectible

获取一个值,该值指示此 MemberInfo 对象是否是包含在可回收的 AssemblyLoadContext 中的程序集的一部分。

(继承自 MemberInfo)
IsConstructedGenericMethod

定义并表示可以编译、执行和丢弃的一种动态方法。 丢弃的方法可用于垃圾回收。

(继承自 MethodBase)
IsConstructor

获取一个值,该值指示此方法是否为构造函数。

(继承自 MethodBase)
IsFamily

获取一个值,该值指示此方法或构造函数的可见性是否由 Family 描述;也就是说,此方法或构造函数仅在其类和派生类内可见。

(继承自 MethodBase)
IsFamilyAndAssembly

获取一个值,该值指示此方法或构造函数的可见性是否由 FamANDAssem 描述;也就是说,此方法或构造函数可由派生类调用,但仅当这些派生类在同一程序集中时。

(继承自 MethodBase)
IsFamilyOrAssembly

获取一个值,该值指示此方法或构造函数的潜在可见性是否由 FamORAssem 描述;也就是说,此方法或构造函数可由派生类(无论其位置如何)和同一程序集中的类调用。

(继承自 MethodBase)
IsFinal

获取一个值,该值指示此方法是否为 final

(继承自 MethodBase)
IsGenericMethod

获取一个值,该值指示当前方法是否为泛型方法。

(继承自 MethodInfo)
IsGenericMethodDefinition

获取一个值,该值指示当前 MethodInfo 是否表示泛型方法的定义。

(继承自 MethodInfo)
IsHideBySig

获取一个值,该值指示是否只有一个签名完全相同的同一种类的成员在派生类中是隐藏的。

(继承自 MethodBase)
IsPrivate

获取一个值,该值指示此成员是否是私有的。

(继承自 MethodBase)
IsPublic

获取一个值,该值指示这是否是一个公共方法。

(继承自 MethodBase)
IsSecurityCritical

获取一个值,该值指示当前的动态方法是否为安全关键或安全可靠关键,以执行关键操作。

IsSecurityCritical

获取一个值,该值指示当前方法或构造函数在当前信任级别上是安全关键的还是安全可靠关键的,因此可以执行关键操作。

(继承自 MethodBase)
IsSecuritySafeCritical

获取一个值,该值指示当前动态方法在当前信任级别上是否是安全可靠关键的;即它是否可以执行关键操作并可以由透明代码访问。

IsSecuritySafeCritical

获取一个值,该值指示当前方法或构造函数在当前信任级别上是安全可靠关键的;即它是否可以执行关键操作并可以由透明代码访问。

(继承自 MethodBase)
IsSecurityTransparent

获取一个值,该值指示当前的动态方法是否在当前的信任级别是透明的,并因此无法执行关键操作。

IsSecurityTransparent

获取一个值,该值指示当前方法或构造函数在当前信任级别上是透明的,因此无法执行关键操作。

(继承自 MethodBase)
IsSpecialName

获取一个值,该值指示此方法是否具有特殊名称。

(继承自 MethodBase)
IsStatic

获取一个值,该值指示方法是否为 static

(继承自 MethodBase)
IsVirtual

获取一个值,该值指示方法是否为 virtual

(继承自 MethodBase)
MemberType

获取一个 MemberTypes 值,该值指示此成员是方法。

(继承自 MethodInfo)
MetadataToken

获取一个值,该值标识元数据元素。

(继承自 MemberInfo)
MethodHandle

不支持动态方法。

MethodHandle

获取方法的内部元数据表示形式的句柄。

(继承自 MethodBase)
MethodImplementationFlags

定义并表示可以编译、执行和丢弃的一种动态方法。 丢弃的方法可用于垃圾回收。

MethodImplementationFlags

获取指定方法实现特性的 MethodImplAttributes 标志。

(继承自 MethodBase)
Module

获取动态方法与之在逻辑上相关联的模块。

Module

获取一个模块,在该模块中已经定义一个类型,该类型用于声明由当前 MemberInfo 表示的成员。

(继承自 MemberInfo)
Name

获取动态方法的名称。

ReflectedType

获取在反射中用于获取该方法的类。

ReflectedType

获取用于获取 MemberInfo 的此实例的类对象。

(继承自 MemberInfo)
ReturnParameter

获取动态方法的返回参数。

ReturnType

获取动态方法的返回值的类型。

ReturnTypeCustomAttributes

获取动态方法的返回类型的自定义属性。

ReturnTypeCustomAttributes

获取返回类型的自定义属性。

(继承自 MethodInfo)

方法

CreateDelegate(Type)

完成动态方法并创建一个可执行此方法的委托。

CreateDelegate(Type, Object)

完成动态方法并创建一个可用于执行该方法的委托,指定委托类型和委托绑定到的对象。

CreateDelegate<T>()

从此方法创建 T 类型的委托。

(继承自 MethodInfo)
CreateDelegate<T>(Object)

从此方法创建具有指定目标的 T 类型的委托。

(继承自 MethodInfo)
DefineParameter(Int32, ParameterAttributes, String)

定义动态方法的参数。

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 MethodInfo)
GetBaseDefinition()

返回方法的基实现。

GetBaseDefinition()

当在派生类中被重写时,为直接或间接的基类(用该实例表示的方法首先在此类中声明)上的方法返回 MethodInfo 对象。

(继承自 MethodInfo)
GetCustomAttributes(Boolean)

返回为该方法定义的所有自定义属性。

GetCustomAttributes(Boolean)

在派生类中重写时,返回应用于此成员的所有自定义属性的数组。

(继承自 MemberInfo)
GetCustomAttributes(Type, Boolean)

返回已应用到此方法的指定类型的自定义属性。

GetCustomAttributes(Type, Boolean)

在派生类中重写时,返回应用于此成员并由 Type 标识的自定义属性的数组。

(继承自 MemberInfo)
GetCustomAttributesData()

返回 CustomAttributeData 对象列表,这些对象表示已应用到目标成员的特性相关数据。

(继承自 MemberInfo)
GetDynamicILInfo()

返回 DynamicILInfo 对象,该对象可以用于从元数据标记、范围和 Microsoft 中间语言 (MSIL) 流中生成方法主体。

GetGenericArguments()

返回 Type 对象的数组,这些对象表示泛型方法的类型实参或泛型方法定义的类型形参。

(继承自 MethodInfo)
GetGenericMethodDefinition()

返回一个 MethodInfo 对象,该对象表示可从其构造当前方法的泛型方法定义。

(继承自 MethodInfo)
GetHashCode()

返回此实例的哈希代码。

(继承自 MethodInfo)
GetILGenerator()

为具有默认 64 字节 Microsoft 中间语言 (MSIL) 流大小的方法返回 MSIL 生成器。

GetILGenerator(Int32)

为方法返回一个具有指定 MSIL 流大小的 Microsoft 中间语言 (MSIL) 生成器。

GetMethodBody()

在派生类中重写后,获取 MethodBody 对象,该对象提供对 MSIL 流、局部变量和当前方法的异常的访问。

(继承自 MethodBase)
GetMethodImplementationFlags()

为此方法返回实现标志。

GetMethodImplementationFlags()

在派生的类中重写时,返回 MethodImplAttributes 标志。

(继承自 MethodBase)
GetParameters()

返回动态方法的参数。

GetType()

发现方法的属性并提供对方法元数据的访问。

(继承自 MethodInfo)
HasSameMetadataDefinitionAs(MemberInfo)

定义并表示可以编译、执行和丢弃的一种动态方法。 丢弃的方法可用于垃圾回收。

(继承自 MemberInfo)
Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

在指定的活页夹的约束下,用指定的区域性信息,使用指定的参数调用动态方法。

Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)

当在派生类中重写时,调用具有给定参数的反射的方法或构造函数。

(继承自 MethodBase)
Invoke(Object, Object[])

使用指定参数调用由当前实例表示的方法或构造函数。

(继承自 MethodInfo)
IsDefined(Type, Boolean)

表示是否定义了指定的自定义属性类型。

IsDefined(Type, Boolean)

在派生类中重写时,指示是否将指定类型或其派生类型的一个或多个特性应用于此成员。

(继承自 MemberInfo)
MakeGenericMethod(Type[])

用类型数组的元素替代当前泛型方法定义的类型参数,并返回表示结果构造方法的 MethodInfo 对象。

(继承自 MethodInfo)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示为字符串的方法的签名。

显式接口实现

_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 MemberInfo)
_MemberInfo.GetType()

获取一个表示 MemberInfo 类的 Type 对象。

(继承自 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 MemberInfo)
_MethodBase.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 MethodBase)
_MethodBase.GetType()

有关此成员的说明,请参见 GetType()

(继承自 MethodBase)
_MethodBase.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 MethodBase)
_MethodBase.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 MethodBase)
_MethodBase.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 MethodBase)
_MethodBase.IsAbstract

有关此成员的说明,请参见 IsAbstract

(继承自 MethodBase)
_MethodBase.IsAssembly

有关此成员的说明,请参见 IsAssembly

(继承自 MethodBase)
_MethodBase.IsConstructor

有关此成员的说明,请参见 IsConstructor

(继承自 MethodBase)
_MethodBase.IsFamily

有关此成员的说明,请参见 IsFamily

(继承自 MethodBase)
_MethodBase.IsFamilyAndAssembly

有关此成员的说明,请参见 IsFamilyAndAssembly

(继承自 MethodBase)
_MethodBase.IsFamilyOrAssembly

有关此成员的说明,请参见 IsFamilyOrAssembly

(继承自 MethodBase)
_MethodBase.IsFinal

有关此成员的说明,请参见 IsFinal

(继承自 MethodBase)
_MethodBase.IsHideBySig

有关此成员的说明,请参见 IsHideBySig

(继承自 MethodBase)
_MethodBase.IsPrivate

有关此成员的说明,请参见 IsPrivate

(继承自 MethodBase)
_MethodBase.IsPublic

有关此成员的说明,请参见 IsPublic

(继承自 MethodBase)
_MethodBase.IsSpecialName

有关此成员的说明,请参见 IsSpecialName

(继承自 MethodBase)
_MethodBase.IsStatic

有关此成员的说明,请参见 IsStatic

(继承自 MethodBase)
_MethodBase.IsVirtual

有关此成员的说明,请参见 IsVirtual

(继承自 MethodBase)
_MethodInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 MethodInfo)
_MethodInfo.GetType()

提供从 COM 对 GetType() 方法的访问。

(继承自 MethodInfo)
_MethodInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 MethodInfo)
_MethodInfo.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 MethodInfo)
_MethodInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 MethodInfo)
ICustomAttributeProvider.GetCustomAttributes(Boolean)

返回在该成员上定义的所有自定义特性的数组(已命名的特性除外),如果没有自定义特性,则返回空数组。

(继承自 MemberInfo)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

返回在该成员上定义、由类型标识的自定义属性数组,如果没有该类型的自定义属性,则返回空数组。

(继承自 MemberInfo)
ICustomAttributeProvider.IsDefined(Type, Boolean)

指示是否在该成员上定义了一个或多个 attributeType 实例。

(继承自 MemberInfo)

扩展方法

GetCustomAttribute(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性。

GetCustomAttribute(MemberInfo, Type, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

GetCustomAttribute<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性。

GetCustomAttribute<T>(MemberInfo, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

GetCustomAttributes(MemberInfo)

检索应用于指定成员的自定义特性集合。

GetCustomAttributes(MemberInfo, Boolean)

检索应用于指定成员的自定义特性集合,并可选择检查该成员的上级。

GetCustomAttributes(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性集合。

GetCustomAttributes(MemberInfo, Type, Boolean)

检索应用于指定成员的指定类型的自定义特性集合,并可选择检查该成员的上级。

GetCustomAttributes<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性集合。

GetCustomAttributes<T>(MemberInfo, Boolean)

检索应用于指定成员的指定类型的自定义特性集合,并可选择检查该成员的上级。

IsDefined(MemberInfo, Type)

确定是否将指定类型的任何自定义属性应用于指定的成员。

IsDefined(MemberInfo, Type, Boolean)

指示一个指定类型的自定义特性是否应用于一个指定的数字,并选择性地应用于其的上级。

GetMetadataToken(MemberInfo)

获取给定成员的元数据令牌(如果可用)。

HasMetadataToken(MemberInfo)

返回表示元数据令牌是否可用于指定的成员的值。

GetBaseDefinition(MethodInfo)

定义并表示可以编译、执行和丢弃的一种动态方法。 丢弃的方法可用于垃圾回收。

GetRuntimeBaseDefinition(MethodInfo)

检索表示在此方法最先声明的直接或间接类上的指定方法的对象。

适用于

另请参阅