Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Megjegyzés:
Ez a cikk kiegészítő megjegyzéseket tartalmaz az API referenciadokumentációjához.
DynamicMethod(String, Type, Type[], Boolean) Konstruktor
A konstruktor által létrehozott dinamikus metódus egy névtelen szerelvényhez van társítva egy meglévő típus vagy modul helyett. A névtelen szerelvény csak azért létezik, hogy tesztkörnyezetet biztosítson a dinamikus metódusokhoz, azaz elkülönítse őket más kódoktól. Ez a környezet biztonságossá teszi, hogy a dinamikus metódust részben megbízható kóddal lehessen kibocsátani és végrehajtani.
A névtelenül üzemeltetett dinamikus metódusok nem férnek hozzá automatikusan a Visual Basicben szereplő típusokhoz vagyprivate tagokhozprotectedinternal.Friend Ez eltér egy meglévő típushoz vagy modulhoz társított dinamikus metódusoktól, amelyek a társított hatókörük rejtett tagjaihoz rendelkeznek hozzáféréssel.
Adja meg a true-t a restrictedSkipVisibility-hez, ha a dinamikus metódusnak hozzá kell férnie azokhoz a típusokhoz vagy tagokhoz, amelyek private, protected, vagy internal. Ez korlátozott hozzáférést biztosít a dinamikus metódusnak ezekhez a tagokhoz. Vagyis a tagok csak akkor érhetők el, ha az alábbi feltételek teljesülnek:
A céltagok olyan szerelvényhez tartoznak, amelynek megbízhatósági szintje egyenlő vagy alacsonyabb, mint a dinamikus metódust kibocsátó hívásverem.
A dinamikus metódust kibocsátó hívásverem a ReflectionPermission jelölővel lesz megadvaReflectionPermissionFlag.RestrictedMemberAccess. Ez mindig igaz, ha a kód teljes megbízhatósággal van végrehajtva. Részlegesen megbízható kód esetén ez csak akkor igaz, ha a gazdagép kifejezetten megadja az engedélyt.
Fontos
Ha az engedély nem lett megadva, a rendszer biztonsági kivételt ad ki, amikor CreateDelegate meghívják, vagy amikor a dinamikus metódust meghívja, és nem a konstruktor meghívásakor. A dinamikus metódus kibocsátásához nincs szükség különleges engedélyekre.
Például egy olyan dinamikus metódus, amely úgy van restrictedSkipVisibility beállítva true, hogy elérhesse a hívásverem bármelyik szerelvényének privát tagját, ha a hívásverem számára korlátozott taghozzáférés engedélyezett. Ha a dinamikus metódust részben megbízható kód hozza létre a hívásveremben, akkor nem fér hozzá egy .NET Framework assembly típusának privát tagjához, mert az ilyen assemblyk teljes bizalmat élveznek.
Ha restrictedSkipVisibilityfalse értéket vesz fel, a JIT láthatósági ellenőrzések érvényesítésre kerülnek. A dinamikus metódus kódjának hozzáférése van a nyilvános osztályok nyilvános metódusaihoz, és kivételeket vált ki, ha olyan típusokhoz vagy tagokhoz próbál hozzáférni, amelyek private, protected vagy internal.
Névtelenül üzemeltetett dinamikus metódus létrehozásakor a kibocsátó szerelvény hívásveremét is tartalmazza. A metódus meghívásakor a rendszer a tényleges hívó engedélyei helyett a kibocsátó hívásverem engedélyeit használja. Így a dinamikus metódus nem hajtható végre magasabb szintű jogosultsággal, mint az azt kibocsátó szerelvényé, még akkor sem, ha azt egy magasabb megbízhatósági szintű szerelvény továbbítja és hajtja végre.
Ez a konstruktor megadja a metódusattribútumokat MethodAttributes.Public és MethodAttributes.Statica hívó konvenciót CallingConventions.Standard.
Megjegyzés:
Ez a konstruktor a .NET-keretrendszer 3.5-ös vagy újabb verziójában lett bevezetve.
DynamicMethod(String, Type, Type[], Module) Konstruktor
Ez a konstruktor adja meg a metódusattribútumokat MethodAttributes.Public és MethodAttributes.Static, a hívási konvenciót CallingConventions.Standard, és nem hagyja ki a just-in-time (JIT) láthatósági ellenőrzéseket.
Az ezzel a konstruktorral létrehozott dinamikus módszer a modulban internaltalálható összes típus nyilvános és Friend (mVisual Basic) tagjaihoz is hozzáfér.
Megjegyzés:
A visszamenőleges kompatibilitás érdekében ez a konstruktor a SecurityPermission jelölővel követeli meg a SecurityPermissionFlag.ControlEvidence-et, ha a következő feltételek teljesülnek: m a hívó modultól eltérő modul, és a ReflectionPermission jelölővel való ReflectionPermissionFlag.MemberAccess igény nem sikeres. Ha a SecurityPermission kérés teljesül, a művelet engedélyezett.
Examples
Az alábbi példakód egy dinamikus metódust hoz létre, amely két paramétert vesz igénybe. A példa egy egyszerű függvénytörzset bocsát ki, amely az első paramétert a konzolra nyomtatja, a példa pedig a második paramétert használja a metódus visszatérési értékeként. A példa egy delegált létrehozásával fejezi be a metódust, meghívja a delegáltat különböző paraméterekkel, és végül meghívja a dinamikus metódust a Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) metódus használatával.
using System;
using System.Reflection;
using System.Reflection.Emit;
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 second argument 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. Further attempts to change the
// method are ignored and no exception is thrown.
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 second argument 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 are ignored and don't throw 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
'
DynamicMethod(String, Type, Type[], Type) Konstruktor
Az ezzel a konstruktorral létrehozott dinamikus módszer hozzáféréssel rendelkezik a owner típus összes tagjához, valamint a modulban található összes többi típus nyilvános és internal (Friend Visual Basic) tagjaihoz, ahol a owner található.
Ez a konstruktor adja meg a metódusattribútumokat MethodAttributes.Public és MethodAttributes.Static, a hívási konvenciót CallingConventions.Standard, és nem hagyja ki a just-in-time (JIT) láthatósági ellenőrzéseket.
Megjegyzés:
A visszamenőleges kompatibilitás érdekében ez a SecurityPermission konstruktor megköveteli a SecurityPermissionFlag.ControlEvidence jelölőt, ha a következő feltételek egyszerre teljesülnek: owner egy másik modulban van, mint a hívó modul, és a ReflectionPermission jelölővel kapcsolatos ReflectionPermissionFlag.MemberAccess igény nem teljesült. Ha a SecurityPermission kérés teljesül, a művelet engedélyezett.
Examples
Az alábbi példakód egy DynamicMethod típushoz logikailag társított típust hoz létre. Ez a társítás hozzáférést biztosít az adott típus magántagjaihoz.
A példakód egy privát mezővel elnevezett Example osztályt határoz meg, egy osztálytDerivedFromExample, amely az első osztályból származik, egy delegált típustUseLikeStatic, amely visszaadja Int32 és típusparaméterekkel Example rendelkezik, valamint Int32egy delegálttípustUseLikeInstance, amely egy adott típusú Int32paramétert ad visszaInt32.
A példakód ezután létrehoz egy DynamicMethod olyan értéket, amely megváltoztatja egy példány Example privát mezőjét, és visszaadja az előző értéket.
Megjegyzés:
Az osztályok belső mezőinek módosítása általában nem jó objektumorientált kódolási gyakorlat.
A példakód létrehozza a(z) Example példányt, majd létrehoz két meghatalmazottat. Az első típus ugyanazokkal UseLikeStatica paraméterekkel rendelkezik, mint a dinamikus metódus. A második típus UseLikeInstance, amely nem rendelkezik az első paraméter (típus Example). Ez a küldött a CreateDelegate(Type, Object) metódus túlterhelésével jön létre; ennek a metódus túlterhelésnek a második paramétere egy Example példány, ebben az esetben az éppen létrehozott példány, amely az újonnan létrehozott delegálthoz van kötve. A delegált meghívásakor a dinamikus metódus a kötött példányon hajtódik végre Example.
Megjegyzés:
Ez egy példa a .NET-keretrendszer 2.0-s verziójában bevezetett delegálási kötés laza szabályaira, valamint a Delegate.CreateDelegate metódus új túlterhelésére. További információ: Delegate osztály.
A UseLikeStatic delegált meghívásra kerül, úgy, hogy átadjuk a Example példányt, amely a UseLikeInstance delegálthoz van kötve. Ezután a UseLikeInstance delegáltat hívják meg annak érdekében, hogy mindkét delegált ugyanazon a Example példányon működjön. A belső mező értékeinek változásai minden hívás után megjelennek. Végül a UseLikeInstance delegált egy példányhoz van kötve DerivedFromExample, és a delegált hívások ismétlődnek.
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'