DynamicMethod Konstruktory

Definicja

Tworzy metodę dynamiczną.

Przeciążenia

DynamicMethod(String, Type, Type[])

Inicjuje anonimowo hostowaną metodę dynamiczną, określając nazwę metody, typ zwracany i typy parametrów.

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

Inicjuje anonimowo hostowaną metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów oraz czy kontrole widoczności just in time (JIT) powinny być pomijane dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni Microsoft (MSIL) metody dynamicznej.

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

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, typ zwracany, typy parametrów i moduł.

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

Tworzy metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów i typ, z którym metoda dynamiczna jest logicznie skojarzona.

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

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, typ zwracany, typy parametrów, moduł oraz to, czy kontrole widoczności just in time (JIT) powinny być pomijane dla typów i elementów członkowskich dostępnych przez język microsoft intermediate language (MSIL) metody dynamicznej.

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

Tworzy metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów, typ, z którym metoda dynamiczna jest logicznie skojarzona i czy kontrole widoczności just in time (JIT) powinny być pomijane dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni microsoft (MSIL) metody dynamicznej.

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

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, atrybuty, konwencję wywoływania, typ zwracany, typy parametrów, moduł i czy kontrole widoczności just in time (JIT) powinny być pomijane dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni microsoft (MSIL) metody dynamicznej.

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

Tworzy metodę dynamiczną, określając nazwę metody, atrybuty, konwencję wywoływania, typ zwracany, typy parametrów, typ, z którym metoda dynamiczna jest logicznie skojarzona i czy kontrole widoczności just in time (JIT) powinny zostać pominięte dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni microsoft (MSIL) metody dynamicznej.

DynamicMethod(String, Type, Type[])

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Inicjuje anonimowo hostowaną metodę dynamiczną, określając nazwę metody, typ zwracany i typy parametrów.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type())

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt, który określa zwracany typ metody dynamicznej lub null jeśli metoda nie ma zwracanego typu.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

Wyjątki

Element elementu parameterTypes to null lub Void.

name to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca wartość true.

Uwagi

Metoda dynamiczna utworzona przez ten konstruktor jest skojarzona z zestawem anonimowym zamiast istniejącego typu lub modułu. Zestaw anonimowy istnieje tylko w celu zapewnienia środowiska piaskownicy dla metod dynamicznych, czyli odizolowania ich od innego kodu. To środowisko sprawia, że metoda dynamiczna może być emitowana i wykonywana przez częściowo zaufany kod.

Ten konstruktor określa, że kontrole widoczności just in time (JIT) będą wymuszane dla języka pośredniego firmy Microsoft (MSIL) metody dynamicznej. Oznacza to, że kod w metodzie dynamicznej ma dostęp do publicznych metod klas publicznych. Wyjątki są zgłaszane, jeśli metoda próbuje uzyskać dostęp do typów lub elementów członkowskich, które są private, protectedlub internal (Friend w Visual Basic). Aby utworzyć metodę dynamiczną, która ma ograniczoną możliwość pomijania kontroli widoczności JIT, użyj konstruktora DynamicMethod(String, Type, Type[], Boolean) .

Gdy tworzona jest anonimowo hostowana metoda dynamiczna, dołączany jest stos wywołań zestawu emitowanego. Po wywołaniu metody uprawnienia zestawu emitujące są używane zamiast uprawnień rzeczywistego obiektu wywołującego. W związku z tym metoda dynamiczna nie może wykonać na wyższym poziomie uprawnień niż zestaw, który go emitował, nawet jeśli jest przekazywany do i wykonywany przez zestaw, który ma wyższy poziom zaufania.

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, oraz konwencję CallingConventions.Standardwywoływania .

Uwaga

Ten konstruktor został wprowadzony w .NET Framework 3.5 lub nowszym.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Inicjuje anonimowo hostowaną metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów oraz czy kontrole widoczności just in time (JIT) powinny być pomijane dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni Microsoft (MSIL) metody dynamicznej.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, bool restrictedSkipVisibility);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes, bool restrictedSkipVisibility);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), restrictedSkipVisibility As Boolean)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt, który określa zwracany typ metody dynamicznej lub null jeśli metoda nie ma zwracanego typu.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

restrictedSkipVisibility
Boolean

true aby pominąć kontrole widoczności JIT typów i elementów członkowskich, do których uzyskuje dostęp MSIL metody dynamicznej, z tym ograniczeniem: poziom zaufania zestawów zawierających te typy i elementy członkowskie muszą być równe lub mniejsze niż poziom zaufania stosu wywołań, który emituje metodę dynamiczną; w przeciwnym razie , false.

Wyjątki

Element elementu parameterTypes to null lub Void.

name to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca wartość true.

Uwagi

Metoda dynamiczna utworzona przez ten konstruktor jest skojarzona z zestawem anonimowym zamiast istniejącego typu lub modułu. Zestaw anonimowy istnieje tylko w celu zapewnienia środowiska piaskownicy dla metod dynamicznych, czyli odizolowania ich od innego kodu. To środowisko sprawia, że metoda dynamiczna może być emitowana i wykonywana przez częściowo zaufany kod.

Metody dynamiczne hostowane anonimowo nie mają automatycznego dostępu do żadnych typów lub elementów członkowskich, które są private, protectedlub internal (Friend w Visual Basic). Różni się to od metod dynamicznych skojarzonych z istniejącym typem lub modułem, które mają dostęp do ukrytych elementów członkowskich w ich skojarzonym zakresie.

Określ true , restrictedSkipVisibility czy metoda dynamiczna musi uzyskiwać dostęp do typów lub elementów członkowskich, które są private, protectedlub internal. Dzięki temu metoda dynamiczna ma ograniczony dostęp do tych elementów członkowskich. Oznacza to, że dostęp do członków można uzyskać tylko wtedy, gdy spełnione są następujące warunki:

  • Docelowe elementy członkowskie należą do zestawu, który ma poziom zaufania równy lub niższy niż stos wywołań, który emituje metodę dynamiczną.

  • Stos wywołań, który emituje metodę dynamiczną, otrzymuje ReflectionPermission flagę ReflectionPermissionFlag.RestrictedMemberAccess . Jest to zawsze prawdziwe, gdy kod jest wykonywany z pełnym zaufaniem. W przypadku częściowo zaufanego kodu jest to prawda tylko wtedy, gdy host jawnie udziela uprawnienia.

    Ważne

    Jeśli uprawnienie nie zostało przyznane, zgłaszany jest wyjątek zabezpieczeń, gdy CreateDelegate jest wywoływany lub gdy jest wywoływana metoda dynamiczna, a nie wtedy, gdy jest wywoływany ten konstruktor. Do emitowania metody dynamicznej nie są wymagane żadne specjalne uprawnienia.

Na przykład metoda dynamiczna, która jest tworzona z zestawem restrictedSkipVisibilitytrue , może uzyskać dostęp do prywatnego elementu członkowskiego dowolnego zestawu w stosie wywołań, jeśli stos wywołań otrzymał ograniczony dostęp do elementu członkowskiego. Jeśli metoda dynamiczna jest tworzona przy użyciu częściowo zaufanego kodu na stosie wywołań, nie może uzyskać dostępu do prywatnego elementu członkowskiego typu w zestawie .NET Framework, ponieważ takie zestawy są w pełni zaufane.

Jeśli restrictedSkipVisibility parametr ma falsewartość , kontrole widoczności JIT są wymuszane. Kod w metodzie dynamicznej ma dostęp do publicznych metod klas publicznych, a wyjątki są zgłaszane, jeśli próbuje uzyskać dostęp do typów lub składowych, które są private, protectedlub internal.

Gdy tworzona jest anonimowo hostowana metoda dynamiczna, dołączany jest stos wywołań zestawu emitowanego. Po wywołaniu metody uprawnienia stosu wywołań emitujących są używane zamiast uprawnień rzeczywistego obiektu wywołującego. W związku z tym metoda dynamiczna nie może wykonać na wyższym poziomie uprawnień niż zestaw, który go emitował, nawet jeśli jest przekazywany do i wykonywany przez zestaw, który ma wyższy poziom zaufania.

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, oraz konwencję CallingConventions.Standardwywoływania .

Uwaga

Ten konstruktor został wprowadzony w .NET Framework 3.5 lub nowszym.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, typ zwracany, typy parametrów i moduł.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes, System.Reflection.Module m);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes, System.Reflection.Module m);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * System.Reflection.Module -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), m As Module)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt, który określa zwracany typ metody dynamicznej lub null jeśli metoda nie ma zwracanego typu.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

m
Module

Reprezentacja Module modułu, z którym ma być logicznie skojarzona metoda dynamiczna.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

m to moduł, który zapewnia anonimowe hostowanie metod dynamicznych.

name to null.

-lub-

m to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca wartość true.

Przykłady

Poniższy przykład kodu tworzy metodę dynamiczną, która przyjmuje dwa parametry. Przykład emituje prostą treść funkcji, która wyświetla pierwszy parametr w konsoli, a w przykładzie użyto drugiego parametru jako wartości zwracanej metody. Przykład kończy metodę, tworząc delegata, wywołuje delegata z różnymi parametrami, a na koniec wywołuje metodę dynamiczną Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) przy użyciu metody .

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

public ref class Test
{   
};

// Declare a delegate that will be used to execute the completed
// dynamic method.
delegate int HelloInvoker(String^ msg, int ret);

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

    // Create a dynamic method with the name "Hello", a return type
    // of int, and two parameters whose types are specified by the
    // array helloArgs. Create the method in the module that
    // defines the Test class.
    DynamicMethod^ hello = gcnew DynamicMethod("Hello", 
        int::typeid,
        helloArgs,
        Test::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.
    ILGenerator^ ilgen = hello->GetILGenerator();
    // Load the first argument, which is a string, onto the stack.
    ilgen->Emit(OpCodes::Ldarg_0);
    // Call the overload of Console.WriteLine that prints a string.
    ilgen->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.
    ilgen->Emit(OpCodes::Ldarg_1);
    ilgen->Emit(OpCodes::Ret);

    // Create a delegate that represents the dynamic method. This
    // action completes the method, and any further attempts to
    // change the method will cause an exception.
    HelloInvoker^ helloDelegate =
        (HelloInvoker^) hello->CreateDelegate(HelloInvoker::typeid);

    // Use the delegate to execute the dynamic method. Save and
    // print the return value.
    int returnValue = helloDelegate("\r\nHello, World!", 42);
    Console::WriteLine("helloDelegate(\"Hello, World!\", 42) returned {0}",
        returnValue);

    // Do it again, with different arguments.
    returnValue = helloDelegate("\r\nHi, Mom!", 5280);
    Console::WriteLine("helloDelegate(\"Hi, Mom!\", 5280) returned {0}",
        returnValue);

    // Create an array of arguments to use with the Invoke method.
    array<Object^>^ delegateArgs = {"\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 ValueType arguments
    // must be boxed.
    Object^ returnValueObject = hello->Invoke(nullptr, delegateArgs);
    Console::WriteLine("hello.Invoke returned {0}", returnValueObject);
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

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

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

        // Create a dynamic method with the name "Hello", a return type
        // of int, and two parameters whose types are specified by the
        // array helloArgs. Create the method in the module that
        // defines the Test class.
        DynamicMethod hello = new DynamicMethod("Hello",
            typeof(int),
            helloArgs,
            typeof(Test).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.
        ILGenerator il = hello.GetILGenerator();
        // 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);

        // Create a delegate that represents the dynamic method. This
        // action completes the method, and any further attempts to
        // change the method will cause an exception.
        HelloInvoker hi =
            (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker));

        // Use the delegate to execute the dynamic method. Save and
        // print the return value.
        int retval = hi("\r\nHello, World!", 42);
        Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}",
            retval);

        // Do it again, with different arguments.
        retval = hi("\r\nHi, Mom!", 5280);
        Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}",
            retval);

        // 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 ValueType arguments
        // must be boxed.
        object objRet = hello.Invoke(null, invokeArgs);
        Console.WriteLine("hello.Invoke returned {0}", objRet);
    }
}
Imports System.Reflection
Imports System.Reflection.Emit

Public Class Test
    ' Declare a delegate that will be used to execute the completed
    ' dynamic method. 
    Private Delegate Function HelloInvoker(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 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 Test class.
        Dim hello As New DynamicMethod("Hello", _
            GetType(Integer), _
            helloArgs, _
            GetType(Test).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.
        Dim il As ILGenerator = hello.GetILGenerator()
        ' 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)

        ' Create a delegate that represents the dynamic method. This
        ' action completes the method, and any further attempts to
        ' change the method will cause an exception.
    Dim hi As HelloInvoker = _
            hello.CreateDelegate(GetType(HelloInvoker))

        ' Use the delegate to execute the dynamic method. Save and
        ' print the return value.
        Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42)
        Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _
            & retval)

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

        ' 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 ValueType arguments
        ' must be boxed. Note that this overload of Invoke is 
        ' inherited from MethodBase, and simply calls the more 
        ' complete overload of Invoke.
        Dim objRet As Object = hello.Invoke(Nothing, invokeArgs)
        Console.WriteLine("hello.Invoke returned " & objRet)
    End Sub
End Class

' This code example produces the following output:
'
'Hello, World!
'Executing delegate hi("Hello, World!", 42) returned 42
'
'Hi, Mom!
'Executing delegate hi("Hi, Mom!", 5280) returned 5280
'
'Hello, World!
'hello.Invoke returned 42
'

Uwagi

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, konwencję CallingConventions.Standardwywoływania i nie pomija kontroli widoczności just in time (JIT).

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do publicznych elementów członkowskich i internal (Friend w języku Visual Basic) wszystkich typów zawartych w module m.

Uwaga

W celu zapewnienia zgodności z poprzednimi wersjami SecurityPermission ten konstruktor wymaga flagi , SecurityPermissionFlag.ControlEvidence jeśli spełnione są następujące warunki: m jest modułem innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermission funkcję z ReflectionPermissionFlag.MemberAccess flagą nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów i typ, z którym metoda dynamiczna jest logicznie skojarzona.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes, Type owner);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * Type -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), owner As Type)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt określający typ zwracany metody dynamicznej lub null jeśli metoda nie ma typu zwracanego.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

owner
Type

Element Type , z którym metoda dynamiczna jest logicznie skojarzona. Metoda dynamiczna ma dostęp do wszystkich elementów członkowskich typu.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

owner jest interfejsem, tablicą, otwartym typem ogólnym lub parametrem typu lub metody ogólnej.

name to null.

-lub-

owner to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca truewartość .

Przykłady

Poniższy przykład kodu tworzy element DynamicMethod , który jest logicznie skojarzony z typem. To skojarzenie zapewnia mu dostęp do prywatnych członków tego typu.

Przykład kodu definiuje klasę o nazwie z polem prywatnym, klasę o nazwieExample, która pochodzi z pierwszej klasy, typ delegata o nazwie DerivedFromExample , który zwraca i ma parametry typu Example i Int32, oraz typ delegata o nazwie UseLikeInstanceUseLikeStatic , który zwraca Int32Int32 i ma jeden parametr typu Int32.

Przykładowy kod tworzy DynamicMethod element, który zmienia pole prywatne wystąpienia i Example zwraca poprzednią wartość.

Uwaga

Ogólnie rzecz biorąc, zmiana wewnętrznych pól klas nie jest dobrą praktyką kodowania zorientowaną na obiekty.

Przykładowy kod tworzy wystąpienie obiektu Example , a następnie tworzy dwa delegaty. Pierwszy jest typu UseLikeStatic, który ma te same parametry co metoda dynamiczna. Drugi jest typu UseLikeInstance, który nie ma pierwszego parametru (typu Example). Ten delegat jest tworzony przy użyciu CreateDelegate(Type, Object) przeciążenia metody; drugi parametr przeciążenia tej metody jest wystąpieniem Example, w tym przypadku właśnie utworzone wystąpienie, które jest powiązane z nowo utworzonym delegatem. Za każdym razem, gdy ten delegat jest wywoływany, metoda dynamiczna działa na powiązanym wystąpieniu .Example

Uwaga

Jest to przykład zrelaksowanych reguł delegowania powiązania wprowadzonego w .NET Framework 2.0 wraz z nowymi przeciążeniami Delegate.CreateDelegate metody. Aby uzyskać więcej informacji, zobacz klasę Delegate .

Delegat UseLikeStatic jest wywoływany, przekazując wystąpienie, Example które jest powiązane z delegatem UseLikeInstance . UseLikeInstance Następnie delegat jest wywoływany, aby oba delegaty działały w tym samym wystąpieniu Example. Zmiany w wartościach pola wewnętrznego są wyświetlane po każdym wywołaniu. UseLikeInstance Na koniec delegat jest powiązany z wystąpieniem obiektu DerivedFromExample, a wywołania delegata są powtarzane.

using System;
using System.Reflection;
using System.Reflection.Emit;

// These classes are for demonstration purposes.
//
public class Example
{
    private int id = 0;
    public Example(int id)
    {
        this.id = id;
    }
    public int ID { get { return id; }}
}

public class DerivedFromExample : Example
{
    public DerivedFromExample(int id) : base(id) {}
}

// Two delegates are declared: UseLikeInstance treats the dynamic
// method as if it were an instance method, and UseLikeStatic
// treats the dynamic method in the ordinary fashion.
//
public delegate int UseLikeInstance(int newID);
public delegate int UseLikeStatic(Example ex, int newID);

public class Demo
{
    public static void Main()
    {
        // This dynamic method changes the private id field. It has
        // no name; it returns the old id value (return type int);
        // it takes two parameters, an instance of Example and
        // an int that is the new value of id; and it is declared
        // with Example as the owner type, so it can access all
        // members, public and private.
        //
        DynamicMethod changeID = new DynamicMethod(
            "",
            typeof(int),
            new Type[] { typeof(Example), typeof(int) },
            typeof(Example)
        );

        // Get a FieldInfo for the private field 'id'.
        FieldInfo fid = typeof(Example).GetField(
            "id",
            BindingFlags.NonPublic | BindingFlags.Instance
        );

        ILGenerator ilg = changeID.GetILGenerator();

        // Push the current value of the id field onto the
        // evaluation stack. It's an instance field, so load the
        // instance of Example before accessing the field.
        ilg.Emit(OpCodes.Ldarg_0);
        ilg.Emit(OpCodes.Ldfld, fid);

        // Load the instance of Example again, load the new value
        // of id, and store the new field value.
        ilg.Emit(OpCodes.Ldarg_0);
        ilg.Emit(OpCodes.Ldarg_1);
        ilg.Emit(OpCodes.Stfld, fid);

        // The original value of the id field is now the only
        // thing on the stack, so return from the call.
        ilg.Emit(OpCodes.Ret);

        // Create a delegate that uses changeID in the ordinary
        // way, as a static method that takes an instance of
        // Example and an int.
        //
        UseLikeStatic uls =
            (UseLikeStatic) changeID.CreateDelegate(
                typeof(UseLikeStatic)
            );

        // Create an instance of Example with an id of 42.
        //
        Example ex = new Example(42);

        // Create a delegate that is bound to the instance of
        // of Example. This is possible because the first
        // parameter of changeID is of type Example. The
        // delegate has all the parameters of changeID except
        // the first.
        UseLikeInstance uli =
            (UseLikeInstance) changeID.CreateDelegate(
                typeof(UseLikeInstance),
                ex
            );

        // First, change the value of id by calling changeID as
        // a static method, passing in the instance of Example.
        //
        Console.WriteLine(
            "Change the value of id; previous value: {0}",
            uls(ex, 1492)
        );

        // Change the value of id again using the delegate bound
        // to the instance of Example.
        //
        Console.WriteLine(
            "Change the value of id; previous value: {0}",
            uli(2700)
        );

        Console.WriteLine("Final value of id: {0}", ex.ID);

        // Now repeat the process with a class that derives
        // from Example.
        //
        DerivedFromExample dfex = new DerivedFromExample(71);

        uli = (UseLikeInstance) changeID.CreateDelegate(
                typeof(UseLikeInstance),
                dfex
            );

        Console.WriteLine(
            "Change the value of id; previous value: {0}",
            uls(dfex, 73)
        );
        Console.WriteLine(
            "Change the value of id; previous value: {0}",
            uli(79)
        );
        Console.WriteLine("Final value of id: {0}", dfex.ID);
    }
}

/* This code example produces the following output:

Change the value of id; previous value: 42
Change the value of id; previous value: 1492
Final value of id: 2700
Change the value of id; previous value: 71
Change the value of id; previous value: 73
Final value of id: 79
 */
Imports System.Reflection
Imports System.Reflection.Emit

' These classes are for demonstration purposes.
'
Public Class Example
    Private _id As Integer = 0
    
    Public Sub New(ByVal newId As Integer) 
        _id = newId    
    End Sub
    
    Public ReadOnly Property ID() As Integer 
        Get
            Return _id
        End Get
    End Property 
End Class

Public Class DerivedFromExample
    Inherits Example
    
    Public Sub New(ByVal newId As Integer) 
        MyBase.New(newId)
    End Sub
End Class
 
' Two delegates are declared: UseLikeInstance treats the dynamic
' method as if it were an instance method, and UseLikeStatic
' treats the dynamic method in the ordinary fashion.
' 
Public Delegate Function UseLikeInstance(ByVal newID As Integer) _
    As Integer 
Public Delegate Function UseLikeStatic(ByVal ex As Example, _
    ByVal newID As Integer) As Integer 

Public Class Demo
    
    Public Shared Sub Main() 
        ' This dynamic method changes the private _id field. It 
        ' has no name; it returns the old _id value (return type 
        ' Integer); it takes two parameters, an instance of Example 
        ' and an Integer that is the new value of _id; and it is 
        ' declared with Example as the owner type, so it can 
        ' access all members, public and private.
        '
        Dim changeID As New DynamicMethod( _
            "", _
            GetType(Integer), _
            New Type() {GetType(Example), GetType(Integer)}, _
            GetType(Example) _
        )
        
        ' Get a FieldInfo for the private field '_id'.
        Dim fid As FieldInfo = GetType(Example).GetField( _
            "_id", _
            BindingFlags.NonPublic Or BindingFlags.Instance _
        )
        
        Dim ilg As ILGenerator = changeID.GetILGenerator()
        
        ' Push the current value of the id field onto the 
        ' evaluation stack. It's an instance field, so load the
        ' instance of Example before accessing the field.
        ilg.Emit(OpCodes.Ldarg_0)
        ilg.Emit(OpCodes.Ldfld, fid)
        
        ' Load the instance of Example again, load the new value 
        ' of id, and store the new field value. 
        ilg.Emit(OpCodes.Ldarg_0)
        ilg.Emit(OpCodes.Ldarg_1)
        ilg.Emit(OpCodes.Stfld, fid)
        
        ' The original value of the id field is now the only 
        ' thing on the stack, so return from the call.
        ilg.Emit(OpCodes.Ret)
        
        
        ' Create a delegate that uses changeID in the ordinary
        ' way, as a static method that takes an instance of
        ' Example and an Integer.
        '
        Dim uls As UseLikeStatic = CType( _
            changeID.CreateDelegate(GetType(UseLikeStatic)), _
            UseLikeStatic _
        )
        
        ' Create an instance of Example with an id of 42.
        '
        Dim ex As New Example(42)
        
        ' Create a delegate that is bound to the instance of 
        ' of Example. This is possible because the first 
        ' parameter of changeID is of type Example. The 
        ' delegate has all the parameters of changeID except
        ' the first.
        Dim uli As UseLikeInstance = CType( _
            changeID.CreateDelegate( _
                GetType(UseLikeInstance), _
                ex), _
            UseLikeInstance _
        )
        
        ' First, change the value of _id by calling changeID as
        ' a static method, passing in the instance of Example.
        '
        Console.WriteLine( _
            "Change the value of _id; previous value: {0}", _
            uls(ex, 1492) _
        )
        
        ' Change the value of _id again using the delegate 
        ' bound to the instance of Example.
        '
        Console.WriteLine( _
            "Change the value of _id; previous value: {0}", _
            uli(2700) _
        )
        
        Console.WriteLine("Final value of _id: {0}", ex.ID)
    

        ' Now repeat the process with a class that derives
        ' from Example.
        '
        Dim dfex As New DerivedFromExample(71)

        uli = CType( _
            changeID.CreateDelegate( _
                GetType(UseLikeInstance), _
                dfex), _
            UseLikeInstance _
        )

        Console.WriteLine( _
            "Change the value of _id; previous value: {0}", _
            uls(dfex, 73) _
        )
        Console.WriteLine( _
            "Change the value of _id; previous value: {0}", _
            uli(79) _
        )
        Console.WriteLine("Final value of _id: {0}", dfex.ID)

    End Sub
End Class

' This code example produces the following output:
'
'Change the value of _id; previous value: 42
'Change the value of _id; previous value: 1492
'Final value of _id: 2700
'Change the value of _id; previous value: 71
'Change the value of _id; previous value: 73
'Final value of _id: 79'

Uwagi

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do wszystkich elementów członkowskich typu owner, oraz do elementów członkowskich publicznych i internal (Friend w Visual Basic) wszystkich innych typów w module zawierającym owner.

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, konwencję wywoływania CallingConventions.Standardi nie pomija kontroli widoczności typu just in time (JIT).

Uwaga

W przypadku zgodności z poprzednimi SecurityPermission wersjami ten konstruktor wymaga flagi, SecurityPermissionFlag.ControlEvidence jeśli spełnione są następujące warunki: owner znajduje się w module innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermissionReflectionPermissionFlag.MemberAccess flagę nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, typ zwracany, typy parametrów, moduł i czy kontrole widoczności just in time (JIT) powinny zostać pominięte dla typów i elementów członkowskich, do których uzyskuje dostęp język microsoft intermediate language (MSIL) metody dynamicznej.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m, bool skipVisibility);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes, System.Reflection.Module m, bool skipVisibility);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * System.Reflection.Module * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), m As Module, skipVisibility As Boolean)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt określający typ zwracany metody dynamicznej lub null jeśli metoda nie ma typu zwracanego.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

m
Module

Reprezentacja Module modułu, z którym metoda dynamiczna ma być logicznie skojarzona.

skipVisibility
Boolean

true Aby pominąć sprawdzanie widoczności JIT typów i elementów członkowskich, do których uzyskuje dostęp MSIL metody dynamicznej.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

m to moduł, który zapewnia anonimowy hosting dla metod dynamicznych.

name to null.

-lub-

m to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca truewartość .

Uwagi

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, i konwencję CallingConventions.Standardwywoływania .

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do publicznych elementów członkowskich i internal (Friend w Visual Basic) wszystkich typów zawartych w module m. Pomijanie kontroli widoczności kompilatora JIT umożliwia dynamicznemu metodzie uzyskiwanie dostępu do prywatnych i chronionych elementów członkowskich wszystkich innych typów. Jest to przydatne, na przykład podczas pisania kodu w celu serializacji obiektów.

Uwaga

W przypadku zgodności z poprzednimi SecurityPermission wersjami ten konstruktor wymaga flagi SecurityPermissionFlag.ControlEvidence , jeśli spełnione są następujące warunki: m jest modułem innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermissionReflectionPermissionFlag.MemberAccess flagę nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, określając nazwę metody, typ zwracany, typy parametrów, typ, z którym metoda dynamiczna jest logicznie skojarzona i czy kontrole widoczności just in time (JIT) powinny zostać pominięte dla typów i elementów członkowskich, do których uzyskuje się dostęp przez język pośredni firmy Microsoft (MSIL) metody dynamicznej.

public:
 DynamicMethod(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner, bool skipVisibility);
public DynamicMethod (string name, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility);
public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * Type * Type[] * Type * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, returnType As Type, parameterTypes As Type(), owner As Type, skipVisibility As Boolean)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

returnType
Type

Type Obiekt określający typ zwracany metody dynamicznej lub null jeśli metoda nie ma typu zwracanego.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

owner
Type

Element Type , z którym metoda dynamiczna jest logicznie skojarzona. Metoda dynamiczna ma dostęp do wszystkich elementów członkowskich typu.

skipVisibility
Boolean

true aby pominąć kontrole widoczności JIT dla typów i elementów członkowskich, do których uzyskuje dostęp MSIL metody dynamicznej; w przeciwnym razie , false.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

owner jest interfejsem, tablicą, otwartym typem ogólnym lub parametrem typu lub metody ogólnej.

name to null.

-lub-

owner to null.

.NET Framework i .NET Core w wersjach starszych niż 2.1: returnType jest typem, dla którego IsByRef zwraca truewartość .

Uwagi

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do wszystkich elementów członkowskich typu owner, oraz do elementów członkowskich publicznych i internal (Friend w Visual Basic) wszystkich innych typów w module zawierającym owner. Pomijanie kontroli widoczności kompilatora JIT umożliwia dynamicznemu metodzie uzyskiwanie dostępu do prywatnych i chronionych elementów członkowskich wszystkich innych typów. Jest to przydatne, na przykład podczas pisania kodu w celu serializacji obiektów.

Ten konstruktor określa atrybuty MethodAttributes.Public metody i MethodAttributes.Static, i konwencję CallingConventions.Standardwywoływania .

Uwaga

W przypadku zgodności z poprzednimi SecurityPermission wersjami ten konstruktor wymaga flagi, SecurityPermissionFlag.ControlEvidence jeśli spełnione są następujące warunki: owner znajduje się w module innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermissionReflectionPermissionFlag.MemberAccess flagę nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, która jest globalna dla modułu, określając nazwę metody, atrybuty, konwencję wywoływania, typ zwracany, typy parametrów, moduł i czy kontrole widoczności just in time (JIT) powinny zostać pominięte dla typów i elementów członkowskich, do których uzyskuje dostęp język pośredni firmy Microsoft (MSIL) metody dynamicznej.

public:
 DynamicMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Reflection::Module ^ m, bool skipVisibility);
public DynamicMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, System.Reflection.Module m, bool skipVisibility);
public DynamicMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Reflection.Module m, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Reflection.Module * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), m As Module, skipVisibility As Boolean)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

attributes
MethodAttributes

Bitowa kombinacja MethodAttributes wartości określających atrybuty metody dynamicznej. Jedyną dozwoloną kombinacją jest Public i Static.

callingConvention
CallingConventions

Konwencja wywoływania metody dynamicznej. Musi mieć wartość Standard.

returnType
Type

Type Obiekt określający typ zwracany metody dynamicznej lub null jeśli metoda nie ma typu zwracanego.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

m
Module

Reprezentacja Module modułu, z którym metoda dynamiczna ma być logicznie skojarzona.

skipVisibility
Boolean

true aby pominąć kontrole widoczności JIT dla typów i elementów członkowskich, do których uzyskuje dostęp MSIL metody dynamicznej; w przeciwnym razie , false.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

m to moduł, który zapewnia anonimowy hosting dla metod dynamicznych.

name to null.

-lub-

m to null.

attributes to kombinacja flag innych niż Public i Static.

-lub-

callingConvention nie Standardjest .

-lub-

returnType to typ, dla którego IsByRef zwraca truewartość .

Uwagi

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do publicznych elementów członkowskich i internal (Friend w Visual Basic) wszystkich typów publicznych i wewnętrznych zawartych w module m.

Pomijanie kontroli widoczności kompilatora JIT umożliwia dynamicznemu metodzie uzyskiwanie dostępu do prywatnych i chronionych elementów członkowskich wszystkich innych typów w module oraz we wszystkich innych zestawach. Jest to przydatne, na przykład podczas pisania kodu w celu serializacji obiektów.

Uwaga

W przypadku zgodności z poprzednimi SecurityPermission wersjami ten konstruktor wymaga flagi SecurityPermissionFlag.ControlEvidence , jeśli spełnione są następujące warunki: m jest modułem innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermissionReflectionPermissionFlag.MemberAccess flagę nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy

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

Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs
Źródło:
DynamicMethod.cs

Tworzy metodę dynamiczną, określając nazwę metody, atrybuty, konwencję wywoływania, typ zwracany, typy parametrów, typ, z którym metoda dynamiczna jest logicznie skojarzona, oraz czy kontrole widoczności just in time (JIT) powinny zostać pominięte dla typów i elementów członkowskich, do których uzyskuje się dostęp przez język pośrednich firmy Microsoft (MSIL) metody dynamicznej.

public:
 DynamicMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, Type ^ owner, bool skipVisibility);
public DynamicMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type owner, bool skipVisibility);
public DynamicMethod (string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility);
new System.Reflection.Emit.DynamicMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type * bool -> System.Reflection.Emit.DynamicMethod
Public Sub New (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), owner As Type, skipVisibility As Boolean)

Parametry

name
String

Nazwa metody dynamicznej. Może to być ciąg o zerowej długości, ale nie może to być null.

attributes
MethodAttributes

Bitowa kombinacja MethodAttributes wartości określających atrybuty metody dynamicznej. Jedyną dozwoloną kombinacją jest Public i Static.

callingConvention
CallingConventions

Konwencja wywoływania metody dynamicznej. Musi mieć wartość Standard.

returnType
Type

Type Obiekt określający typ zwracany metody dynamicznej lub null jeśli metoda nie ma typu zwracanego.

parameterTypes
Type[]

Tablica Type obiektów określających typy parametrów metody dynamicznej lub null jeśli metoda nie ma parametrów.

owner
Type

Element Type , z którym metoda dynamiczna jest logicznie skojarzona. Metoda dynamiczna ma dostęp do wszystkich elementów członkowskich typu.

skipVisibility
Boolean

true aby pominąć kontrole widoczności JIT dla typów i elementów członkowskich, do których uzyskuje dostęp MSIL metody dynamicznej; w przeciwnym razie , false.

Wyjątki

Element elementu parameterTypes to null lub Void.

-lub-

owner jest interfejsem, tablicą, otwartym typem ogólnym lub parametrem typu lub metody ogólnej.

name to null.

-lub-

owner to null.

attributes to kombinacja flag innych niż Public i Static.

-lub-

callingConvention nie Standardjest .

-lub-

returnType to typ, dla którego IsByRef zwraca truewartość .

Uwagi

Metoda dynamiczna jest globalna dla modułu zawierającego typ owner. Ma dostęp do wszystkich elementów członkowskich typu owner.

Metoda dynamiczna utworzona za pomocą tego konstruktora ma dostęp do wszystkich elementów członkowskich typu owner, oraz do elementów członkowskich publicznych i internal (Friend w Visual Basic) wszystkich typów zawartych w module zawierającym owner. Pomijanie kontroli widoczności kompilatora JIT umożliwia dynamicznemu metodzie uzyskiwanie dostępu do prywatnych i chronionych elementów członkowskich wszystkich innych typów. Jest to przydatne, na przykład podczas pisania kodu w celu serializacji obiektów.

Uwaga

W przypadku zgodności z poprzednimi SecurityPermission wersjami ten konstruktor wymaga flagi, SecurityPermissionFlag.ControlEvidence jeśli spełnione są następujące warunki: owner znajduje się w module innym niż moduł wywołujący, a zapotrzebowanie na ReflectionPermissionReflectionPermissionFlag.MemberAccess flagę nie powiodło się. Jeśli zapotrzebowanie na SecurityPermission powodzenie zakończy się powodzeniem, operacja jest dozwolona.

Zobacz też

Dotyczy