|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
DynamicMethod-Klasse
System.Reflection.MemberInfo
System.Reflection.MethodBase
System.Reflection.MethodInfo
System.Reflection.Emit.DynamicMethod
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Der DynamicMethod-Typ macht die folgenden Member verfügbar.
| Name | Beschreibung | |
|---|---|---|
![]() | DynamicMethod(String, Type, Type[]) | |
![]() | DynamicMethod(String, Type, Type[], Boolean) | |
![]() | DynamicMethod(String, Type, Type[], Module) | |
![]() | DynamicMethod(String, Type, Type[], Type) | |
![]() | DynamicMethod(String, Type, Type[], Module, Boolean) | |
![]() | DynamicMethod(String, Type, Type[], Type, Boolean) | |
![]() | DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Module, Boolean) | |
![]() | DynamicMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type, Boolean) |
| Name | Beschreibung | |
|---|---|---|
![]() | Attributes | |
![]() | CallingConvention | |
![]() | ContainsGenericParameters | |
![]() | CustomAttributes | |
![]() | DeclaringType | |
![]() | InitLocals | |
![]() | IsAbstract | |
![]() | IsAssembly | |
![]() | IsConstructor | |
![]() | IsFamily | |
![]() | IsFamilyAndAssembly | |
![]() | IsFamilyOrAssembly | |
![]() | IsFinal | |
![]() | IsGenericMethod | |
![]() | IsGenericMethodDefinition | |
![]() | IsHideBySig | |
![]() | IsPrivate | |
![]() | IsPublic | |
![]() | IsSecurityCritical | |
![]() | IsSecuritySafeCritical | |
![]() | IsSecurityTransparent | |
![]() | IsSpecialName | |
![]() | IsStatic | |
![]() | IsVirtual | |
![]() | MemberType | |
![]() | MetadataToken | |
![]() | MethodHandle | |
![]() | MethodImplementationFlags | |
![]() | Module | |
![]() | Name | |
![]() | ReflectedType | |
![]() | ReturnParameter | |
![]() | ReturnType | |
![]() | ReturnTypeCustomAttributes |
| Name | Beschreibung | |
|---|---|---|
![]() | CreateDelegate(Type) | |
![]() | CreateDelegate(Type, Object) | |
![]() | DefineParameter | |
![]() | Equals | |
![]() | GetBaseDefinition | |
![]() | GetCustomAttributes(Boolean) | |
![]() | GetCustomAttributes(Type, Boolean) | |
![]() | GetCustomAttributesData | |
![]() | GetDynamicILInfo | |
![]() | GetGenericArguments | |
![]() | GetGenericMethodDefinition | |
![]() | GetHashCode | |
![]() | GetILGenerator() | |
![]() | GetILGenerator(Int32) | |
![]() | GetMethodBody | |
![]() | GetMethodImplementationFlags | |
![]() | GetParameters | |
![]() | GetType | |
![]() | Invoke(Object, Object[]) | |
![]() | Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) | |
![]() | IsDefined | |
![]() | MakeGenericMethod | |
![]() | ToString |
| Name | Beschreibung | |
|---|---|---|
![]() | GetCustomAttribute(Type) | Überladen. |
![]() | GetCustomAttribute(Type, Boolean) | Überladen. |
![]() | GetCustomAttribute<T>() | Überladen. |
![]() | GetCustomAttribute<T>(Boolean) | Überladen. |
![]() | GetCustomAttributes() | Überladen. |
![]() | GetCustomAttributes(Type) | Überladen. |
![]() | GetCustomAttributes<T>() | Überladen. |
![]() | GetCustomAttributes<T>(Boolean) | Überladen. |
![]() | GetRuntimeBaseDefinition | |
![]() | IsDefined |
| Name | Beschreibung | |
|---|---|---|
![]() ![]() | _MemberInfo.GetIDsOfNames | |
![]() ![]() | _MemberInfo.GetType | |
![]() ![]() | _MemberInfo.GetTypeInfo | |
![]() ![]() | _MemberInfo.GetTypeInfoCount | |
![]() ![]() | _MemberInfo.Invoke | |
![]() ![]() | _MethodBase.GetIDsOfNames | |
![]() ![]() | _MethodBase.GetType | |
![]() ![]() | _MethodBase.GetTypeInfo | |
![]() ![]() | _MethodBase.GetTypeInfoCount | |
![]() ![]() | _MethodBase.Invoke | |
![]() ![]() | _MethodBase.IsAbstract | |
![]() ![]() | _MethodBase.IsAssembly | |
![]() ![]() | _MethodBase.IsConstructor | |
![]() ![]() | _MethodBase.IsFamily | |
![]() ![]() | _MethodBase.IsFamilyAndAssembly | |
![]() ![]() | _MethodBase.IsFamilyOrAssembly | |
![]() ![]() | _MethodBase.IsFinal | |
![]() ![]() | _MethodBase.IsHideBySig | |
![]() ![]() | _MethodBase.IsPrivate | |
![]() ![]() | _MethodBase.IsPublic | |
![]() ![]() | _MethodBase.IsSpecialName | |
![]() ![]() | _MethodBase.IsStatic | |
![]() ![]() | _MethodBase.IsVirtual | |
![]() ![]() | _MethodInfo.GetIDsOfNames | |
![]() ![]() | _MethodInfo.GetType | |
![]() ![]() | _MethodInfo.GetTypeInfo | |
![]() ![]() | _MethodInfo.GetTypeInfoCount | |
![]() ![]() | _MethodInfo.Invoke |
Wenn die dynamische Methode anonym gehostet wird, befindet sie sich in einer vom System bereitgestellten Assembly und ist damit von anderem Code isoliert. Standardmäßig erhält sie keinen Zugriff auf nicht öffentliche Daten. Eine anonym gehostete dynamische Methode kann über eine eingeschränkte Fähigkeit zum Überspringen der Sichtbarkeitsprüfungen des JIT-Compilers verfügen, wenn der Methode ReflectionPermission mit dem ReflectionPermissionFlag.RestrictedMemberAccess-Flag gewährt wurde. Die Vertrauensebene der Assembly, auf deren nicht öffentliche Member die dynamische Methode zugreift, muss identisch sein mit oder eine Teilmenge der Vertrauensebene der Aufrufliste darstellen, die die dynamische Methode ausgegeben hat. Weitere Informationen zu anonym gehosteten dynamischen Methoden finden Sie unter Exemplarische Vorgehensweise: Ausgeben von Code in Szenarien mit teilweiser Vertrauenswürdigkeit. Wenn die dynamische Methode einem von Ihnen angegebenen Modul zugeordnet ist, ist die dynamische Methode in diesem Modul global. Sie kann auf alle Typen im Modul und auf alle internal-Member (Friend in Visual Basic) der Typen zugreifen. Sie können eine dynamische Methode jedem Modul zuordnen, unabhängig davon, ob Sie das Modul erstellt haben. Voraussetzung ist lediglich, dass die Aufrufliste, in der sich der Code befindet, eine ReflectionPermission-Anforderung mit dem RestrictedMemberAccess-Flag bereitstellen kann. Wenn die Berechtigung das ReflectionPermissionFlag.MemberAccess-Flag beinhaltet, kann die dynamische Methode die Sichtbarkeitsprüfungen des JIT-Compilers überspringen und auf die privaten Daten aller Typen zugreifen, die im Modul oder in einem beliebigen anderen Modul in einer beliebigen Assembly deklariert wurden.
HinweisWenn Sie das Modul angeben, dem eine dynamische Methode zugeordnet ist, darf dieses Modul nicht in der vom System bereitgestellten Assembly enthalten sein, die für anonymes Hosting verwendet wird. Wenn die dynamische Methode einem von Ihnen angegebenen Typ zugeordnet ist, kann sie unabhängig von der Zugriffsebene auf alle Member des Typs zugreifen. Außerdem können JIT-Sichtbarkeitsprüfungen übersprungen werden. Hierdurch erhält die dynamische Methode Zugriff auf die privaten Daten anderer Typen, die in demselben Modul oder einem beliebigen anderen Modul in einer beliebigen Assembly deklariert wurden. Sie können eine dynamische Methode jedem Typ zuordnen, dem Code muss jedoch ReflectionPermission mit dem RestrictedMemberAccess-Flag und dem MemberAccess-Flag gewährt werden.
| ||
Hinweis |
|---|
| ||
Hinweis |
|---|
Überprüfung
Eine dynamische Methode, die einer sicherheitskritischen Assembly zugeordnet ist, ist auch sicherheitskritisch und kann die Überprüfung überspringen. Zum Beispiel wird eine Assembly ohne Sicherheitsattribute, die als Desktopanwendung ausgeführt wird, von der Laufzeit als sicherheitskritisch behandelt. Wenn Sie der Assembly eine dynamische Methode zuordnen, kann die dynamische Methode nicht überprüfbaren Code enthalten. Wenn eine dynamische Methode, die nicht überprüfbaren Code enthält, einer Assembly zugeordnet wird, die über Transparenz der Ebene 1 verfügt, fügt der Just-In-Time (JIT)-Compiler eine Sicherheitsanforderung ein. Die Forderung ist nur erfolgreich, wenn die dynamische Methode von voll vertrauenswürdigem Code ausgeführt wird. Weitere Informationen finden Sie unter Sicherheitstransparenter Code, Ebene 1. Wenn eine dynamische Methode, die nicht überprüfbaren Code enthält, einer Assembly zugeordnet wird, die über Transparenz der Ebene 2 verfügt, beispielsweise mscorlib.dll, wird eine (vom JIT-Compiler eingefügte) Ausnahme ausgelöst und keine Sicherheitsanforderung vorgenommen. Weitere Informationen finden Sie unter Sicherheitstransparenter Code, Ebene 2. Eine anonym gehostete dynamische Methode, die nicht überprüfbaren Code enthält, löst immer eine Ausnahme aus. Es kann nie die Überprüfung überspringen, auch wenn es durch voll vertrauenswürdigen Code erstellt und ausgeführt wird.
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 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core-Rolle wird nicht unterstützt), Windows Server 2008 R2 (Server Core-Rolle wird mit SP1 oder höher unterstützt; Itanium wird nicht unterstützt)
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.
