diff --git a/src/libraries/System.Private.CoreLib/samples/Directory.Build.props b/src/libraries/System.Private.CoreLib/samples/Directory.Build.props
new file mode 100644
index 00000000000000..d541fc3a2c6e17
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/Directory.Build.props
@@ -0,0 +1,11 @@
+
+
+
+ net11.0
+ false
+ false
+ false
+ false
+ false
+
+
diff --git a/src/libraries/System.Private.CoreLib/samples/Directory.Build.targets b/src/libraries/System.Private.CoreLib/samples/Directory.Build.targets
new file mode 100644
index 00000000000000..8c119d5413b585
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/Directory.Build.targets
@@ -0,0 +1,2 @@
+
+
diff --git a/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.CtorOwnerType.cs b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.CtorOwnerType.cs
new file mode 100644
index 00000000000000..4443aaa80f733a
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.CtorOwnerType.cs
@@ -0,0 +1,57 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// Example: Create a DynamicMethod with an owner type to access private members.
+// Shows CreateDelegate with a bound instance (instance-style invocation).
+// Run: dotnet run DynamicMethod.CtorOwnerType.cs
+
+using System.Reflection;
+using System.Reflection.Emit;
+
+OwnerTypeAccess();
+
+Console.WriteLine("Passed.");
+return 0;
+
+void OwnerTypeAccess()
+{
+ // A DynamicMethod associated with a type can access its private members.
+ DynamicMethod changeID = new(
+ "", typeof(int), [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 current value of 'id' onto the stack.
+ ilg.Emit(OpCodes.Ldarg_0);
+ ilg.Emit(OpCodes.Ldfld, fid);
+ // Store the new value.
+ ilg.Emit(OpCodes.Ldarg_0);
+ ilg.Emit(OpCodes.Ldarg_1);
+ ilg.Emit(OpCodes.Stfld, fid);
+ // Return the old value.
+ ilg.Emit(OpCodes.Ret);
+
+ // Static-style delegate: takes (Example, int), returns old id.
+ var setId = changeID.CreateDelegate>();
+
+ Example ex = new(42);
+ int oldId = setId(ex, 1492);
+ Console.WriteLine($"Previous id: {oldId}, new id: {ex.ID}");
+
+ // Instance-style delegate: bind to a specific Example instance.
+ var setBound = (Func)changeID.CreateDelegate(typeof(Func), ex);
+ oldId = setBound(2700);
+ Console.WriteLine($"Previous id: {oldId}, new id: {ex.ID}");
+
+ // Verify
+ if (ex.ID != 2700)
+ throw new Exception($"FAIL: expected 2700, got {ex.ID}");
+}
+
+// Helper types
+public class Example(int id)
+{
+ private int id = id;
+ public int ID => id;
+}
diff --git a/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Examples.cs b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Examples.cs
new file mode 100644
index 00000000000000..ed0eec92e4518e
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Examples.cs
@@ -0,0 +1,92 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// Runnable examples for DynamicMethod XML doc comments.
+// Each local function is a self-contained sample referenced by tags in DynamicMethod.cs.
+// Run: dotnet run DynamicMethod.Examples.cs
+
+using System.Globalization;
+using System.Reflection;
+using System.Reflection.Emit;
+
+MetadataAndProperties();
+DefineParameterAndGetParameters();
+GetILGeneratorAndInvoke();
+
+Console.WriteLine("All examples passed.");
+return 0;
+
+void MetadataAndProperties()
+{
+ // Create a dynamic method associated with a module.
+ DynamicMethod hello = new("Hello", typeof(int), [typeof(string), typeof(int)], typeof(string).Module);
+
+ // Name: the name specified at creation.
+ Console.WriteLine($"Name: {hello.Name}");
+
+ // DeclaringType is always null for dynamic methods.
+ Console.WriteLine($"DeclaringType: {hello.DeclaringType?.ToString() ?? "(null)"}");
+
+ // ReflectedType is always null for dynamic methods.
+ Console.WriteLine($"ReflectedType: {hello.ReflectedType?.ToString() ?? "(null)"}");
+
+ // Module: the module the dynamic method is associated with.
+ Console.WriteLine($"Module: {hello.Module}");
+
+ // Attributes are always Public | Static.
+ Console.WriteLine($"Attributes: {hello.Attributes}");
+
+ // CallingConvention is always Standard.
+ Console.WriteLine($"CallingConvention: {hello.CallingConvention}");
+
+ // ReturnType: the return type specified at creation.
+ Console.WriteLine($"ReturnType: {hello.ReturnType}");
+
+ // ReturnTypeCustomAttributes: no way to set custom attributes on the return type.
+ ICustomAttributeProvider caProvider = hello.ReturnTypeCustomAttributes;
+ object[] returnAttributes = caProvider.GetCustomAttributes(true);
+ Console.WriteLine($"Return type custom attributes: {returnAttributes.Length}");
+
+ // InitLocals defaults to true — local variables are zero-initialized.
+ Console.WriteLine($"InitLocals: {hello.InitLocals}");
+
+ // ToString returns the method signature (return type, name, parameter types).
+ Console.WriteLine($"ToString: {hello.ToString()}");
+}
+
+void DefineParameterAndGetParameters()
+{
+ DynamicMethod hello = new("Hello", typeof(int), [typeof(string), typeof(int)], typeof(string).Module);
+
+ // DefineParameter adds metadata such as name and attributes.
+ // Parameter positions are 1-based; position 0 refers to the return value.
+ hello.DefineParameter(1, ParameterAttributes.In, "message");
+ hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn");
+
+ // GetParameters retrieves the parameter info.
+ ParameterInfo[] parameters = hello.GetParameters();
+ foreach (ParameterInfo p in parameters)
+ Console.WriteLine($" Param: {p.Name}, {p.ParameterType}, {p.Attributes}");
+}
+
+void GetILGeneratorAndInvoke()
+{
+ DynamicMethod hello = new("Hello", typeof(int), [typeof(string), typeof(int)], typeof(string).Module);
+ MethodInfo writeString = typeof(Console).GetMethod("WriteLine", [typeof(string)])!;
+
+ // GetILGenerator returns an ILGenerator for emitting the method body.
+ ILGenerator il = hello.GetILGenerator();
+ il.Emit(OpCodes.Ldarg_0); // push first arg (string)
+ il.EmitCall(OpCodes.Call, writeString, null); // Console.WriteLine(string)
+ il.Emit(OpCodes.Ldarg_1); // push second arg (int)
+ il.Emit(OpCodes.Ret); // return it
+
+ // CreateDelegate produces a strongly-typed delegate for the dynamic method.
+ Func hi = hello.CreateDelegate>();
+ int retval = hi("Hello from delegate!", 42);
+ Console.WriteLine($"Delegate returned: {retval}");
+
+ // Invoke calls the dynamic method via reflection (slower than a delegate).
+ object? objRet = hello.Invoke(null, BindingFlags.ExactBinding, null,
+ ["Hello from Invoke!", 99], CultureInfo.InvariantCulture);
+ Console.WriteLine($"Invoke returned: {objRet}");
+}
diff --git a/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Overview.cs b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Overview.cs
new file mode 100644
index 00000000000000..453b9eff5bf9c1
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Overview.cs
@@ -0,0 +1,42 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// Example: Create a DynamicMethod, emit IL, and execute via delegate and Invoke.
+// Referenced by DynamicMethod class overview and GetILGenerator/constructor docs.
+// Run: dotnet run DynamicMethod.Overview.cs
+
+using System.Reflection;
+using System.Reflection.Emit;
+
+CreateAndInvoke();
+
+Console.WriteLine("Passed.");
+return 0;
+
+void CreateAndInvoke()
+{
+ // Create a dynamic method with return type int and two parameters (string, int).
+ DynamicMethod hello = new("Hello", typeof(int), [typeof(string), typeof(int)], typeof(string).Module);
+
+ // Emit a body: print the string argument, then return the int argument.
+ MethodInfo writeString = typeof(Console).GetMethod("WriteLine", [typeof(string)])!;
+ ILGenerator il = hello.GetILGenerator();
+ il.Emit(OpCodes.Ldarg_0);
+ il.EmitCall(OpCodes.Call, writeString, null);
+ il.Emit(OpCodes.Ldarg_1);
+ il.Emit(OpCodes.Ret);
+
+ // Create a delegate that represents the dynamic method.
+ Func hi = hello.CreateDelegate>();
+
+ // Execute via delegate.
+ int retval = hi("Hello, World!", 42);
+ Console.WriteLine($"Delegate returned: {retval}");
+
+ // Execute via Invoke (slower — requires boxing and array allocation).
+ object? objRet = hello.Invoke(null, ["Hello via Invoke!", 99]);
+ Console.WriteLine($"Invoke returned: {objRet}");
+
+ // Verify results
+ if (retval != 42 || objRet is not 99)
+ throw new Exception("FAIL: unexpected return values");
+}
diff --git a/src/libraries/System.Private.CoreLib/samples/samples.json b/src/libraries/System.Private.CoreLib/samples/samples.json
new file mode 100644
index 00000000000000..3de27c911d92cd
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/samples/samples.json
@@ -0,0 +1,22 @@
+{
+ "T:System.Reflection.Emit.DynamicMethod": { "file": "System/Reflection/Emit/DynamicMethod.Overview.cs", "method": "CreateAndInvoke", "title": "Creating and invoking a DynamicMethod" },
+ "M:System.Reflection.Emit.DynamicMethod.#ctor(System.String,System.Type,System.Type[],System.Reflection.Module)": { "file": "System/Reflection/Emit/DynamicMethod.Overview.cs", "method": "CreateAndInvoke", "title": "Creating a DynamicMethod associated with a module" },
+ "M:System.Reflection.Emit.DynamicMethod.#ctor(System.String,System.Type,System.Type[],System.Type)": { "file": "System/Reflection/Emit/DynamicMethod.CtorOwnerType.cs", "method": "OwnerTypeAccess", "title": "Creating a DynamicMethod with an owner type" },
+ "P:System.Reflection.Emit.DynamicMethod.Name": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.DeclaringType": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.ReflectedType": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.Module": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.Attributes": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.CallingConvention": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.ReturnType": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.ReturnTypeCustomAttributes": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "P:System.Reflection.Emit.DynamicMethod.InitLocals": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "M:System.Reflection.Emit.DynamicMethod.ToString": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "MetadataAndProperties", "title": "DynamicMethod metadata properties" },
+ "M:System.Reflection.Emit.DynamicMethod.GetParameters": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "DefineParameterAndGetParameters", "title": "Defining and retrieving DynamicMethod parameters" },
+ "M:System.Reflection.Emit.DynamicMethod.DefineParameter(System.Int32,System.Reflection.ParameterAttributes,System.String)": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "DefineParameterAndGetParameters", "title": "Defining and retrieving DynamicMethod parameters" },
+ "M:System.Reflection.Emit.DynamicMethod.GetILGenerator": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "GetILGeneratorAndInvoke", "title": "Emitting IL and invoking a DynamicMethod" },
+ "M:System.Reflection.Emit.DynamicMethod.GetILGenerator(System.Int32)": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "GetILGeneratorAndInvoke", "title": "Emitting IL and invoking a DynamicMethod" },
+ "M:System.Reflection.Emit.DynamicMethod.CreateDelegate(System.Type)": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "GetILGeneratorAndInvoke", "title": "Emitting IL and invoking a DynamicMethod" },
+ "M:System.Reflection.Emit.DynamicMethod.CreateDelegate(System.Type,System.Object)": { "file": "System/Reflection/Emit/DynamicMethod.CtorOwnerType.cs", "method": "OwnerTypeAccess", "title": "Creating a bound delegate from a DynamicMethod" },
+ "M:System.Reflection.Emit.DynamicMethod.Invoke(System.Object,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object[],System.Globalization.CultureInfo)": { "file": "System/Reflection/Emit/DynamicMethod.Examples.cs", "method": "GetILGeneratorAndInvoke", "title": "Emitting IL and invoking a DynamicMethod" }
+}
diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
index 60b3586d375986..fa920b0ded79d2 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs
@@ -10,6 +10,19 @@
namespace System.Reflection.Emit
{
+ ///
+ /// Defines and represents a dynamic method that can be compiled, executed, and discarded. Discarded methods are available for garbage collection.
+ ///
+ ///
+ /// For more information about this API, see Supplemental API remarks for DynamicMethod.
+ ///
+ ///
+ /// The following example creates a dynamic method, emits a method body, and executes it via a delegate and via .
+ ///
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
+ /// Walkthrough: Emitting Code in Partial Trust Scenarios
public sealed partial class DynamicMethod : MethodInfo
{
// The context when the method was created. We use this to do the RestrictedMemberAccess checks.
@@ -25,6 +38,27 @@ public sealed partial class DynamicMethod : MethodInfo
// class initialization (ctor and init)
//
+ ///
+ /// Initializes an anonymously hosted dynamic method, specifying the method name, return type, and parameter types.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// An element of is or .
+ /// is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ /// This constructor was introduced in the .NET Framework 3.5 or later.
+ ///
+ /// The dynamic method that is created by this constructor is associated with an anonymous assembly instead of an existing type or module. The anonymous assembly exists only to provide a sandbox environment for dynamic methods, that is, to isolate them from other code. This environment makes it safe for the dynamic method to be emitted and executed by partially trusted code.
+ /// This constructor specifies that just-in-time (JIT) visibility checks will be enforced for the Microsoft intermediate language (MSIL) of the dynamic method. That is, the code in the dynamic method has access to public methods of public classes. Exceptions are thrown if the method tries to access types or members that are private, protected, or internal (Friend in Visual Basic). To create a dynamic method that has restricted ability to skip JIT visibility checks, use the constructor.
+ /// When an anonymously hosted dynamic method is constructed, the call stack of the emitting assembly is included. When the method is invoked, the permissions of the emitting assembly are used instead of the permissions of the actual caller. Thus, the dynamic method cannot execute at a higher level of privilege than that of the assembly that emitted it, even if it is passed to and executed by an assembly that has a higher trust level.
+ /// This constructor specifies the method attributes Public and Static, and the calling convention Standard.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
+ /// Walkthrough: Emitting Code in Partial Trust Scenarios
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -41,6 +75,36 @@ public DynamicMethod(string name,
true);
}
+ ///
+ /// Initializes an anonymously hosted dynamic method, specifying the method name, return type, parameter types, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method, with this restriction: the trust level of the assemblies that contain those types and members must be equal to or less than the trust level of the call stack that emits the dynamic method; otherwise, .
+ /// An element of is or .
+ /// is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ ///
+ ///
+ /// This constructor was introduced in the .NET Framework 3.5 or later.
+ ///
+ /// The dynamic method that is created by this constructor is associated with an anonymous assembly instead of an existing type or module. The anonymous assembly exists only to provide a sandbox environment for dynamic methods, that is, to isolate them from other code. This environment makes it safe for the dynamic method to be emitted and executed by partially trusted code.
+ /// Anonymously hosted dynamic methods do not have automatic access to any types or members that are private, protected, or internal (Friend in Visual Basic). This is different from dynamic methods that are associated with an existing type or module, which have access to hidden members in their associated scope.
+ /// Specify true for restrictedSkipVisibility if your dynamic method has to access types or members that are private, protected, or internal. This gives the dynamic method restricted access to these members. That is, the members can be accessed only if the following conditions are met:
+ /// - The target members belong to an assembly that has a level of trust equal to or lower than the call stack that emits the dynamic method.
+ /// - The call stack that emits the dynamic method is granted with the RestrictedMemberAccess flag. This is always true when the code is executed with full trust. For partially trusted code, it is true only if the host explicitly grants the permission.
+ /// > If the permission has not been granted, a security exception is thrown when is called or when the dynamic method is invoked, not when this constructor is called. No special permissions are required to emit the dynamic method.
+ /// For example, a dynamic method that is created with restrictedSkipVisibility set to true can access a private member of any assembly on the call stack if the call stack has been granted restricted member access. If the dynamic method is created with partially trusted code on the call stack, it cannot access a private member of a type in a .NET Framework assembly, because such assemblies are fully trusted.
+ /// If restrictedSkipVisibility is false, JIT visibility checks are enforced. The code in the dynamic method has access to public methods of public classes, and exceptions are thrown if it tries to access types or members that are private, protected, or internal.
+ /// When an anonymously hosted dynamic method is constructed, the call stack of the emitting assembly is included. When the method is invoked, the permissions of the emitting call stack are used instead of the permissions of the actual caller. Thus, the dynamic method cannot execute at a higher level of privilege than that of the assembly that emitted it, even if it is passed to and executed by an assembly that has a higher trust level.
+ /// This constructor specifies the method attributes Public and Static, and the calling convention Standard.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
+ /// Walkthrough: Emitting Code in Partial Trust Scenarios
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -58,6 +122,29 @@ public DynamicMethod(string name,
true);
}
+ ///
+ /// Creates a dynamic method that is global to a module, specifying the method name, return type, parameter types, and module.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A representing the module with which the dynamic method is to be logically associated.
+ /// An element of is or . -or- is a module that provides anonymous hosting for dynamic methods.
+ /// is . -or- is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ ///
+ /// This constructor specifies method attributes Public and Static, calling convention Standard, and does not skip just-in-time (JIT) visibility checks.
+ /// The dynamic method created with this constructor has access to public and internal (Friend in Visual Basic) members of all the types contained in module m.
+ /// > For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: m is a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ /// The following code example creates a dynamic method that takes two parameters. The example emits a simple function body that prints the first parameter to the console, and the example uses the second parameter as the return value of the method. The example completes the method by creating a delegate, invokes the delegate with different parameters, and finally invokes the dynamic method using the method.
+ ///
+ ///
+ ///
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -77,6 +164,26 @@ public DynamicMethod(string name,
false);
}
+ ///
+ /// Creates a dynamic method that is global to a module, specifying the method name, return type, parameter types, module, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A representing the module with which the dynamic method is to be logically associated.
+ /// to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method.
+ /// An element of is or . -or- is a module that provides anonymous hosting for dynamic methods.
+ /// is . -or- is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ /// For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: m is a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ ///
+ /// This constructor specifies method attributes Public and Static, and calling convention Standard.
+ /// The dynamic method created with this constructor has access to public and internal (Friend in Visual Basic) members of all the types in contained module m. Skipping the JIT compiler's visibility checks allows the dynamic method to access private and protected members of all other types as well. This is useful, for example, when writing code to serialize objects.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -97,6 +204,28 @@ public DynamicMethod(string name,
false);
}
+ ///
+ /// Creates a dynamic method that is global to a module, specifying the method name, attributes, calling convention, return type, parameter types, module, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A bitwise combination of values that specifies the attributes of the dynamic method. The only combination allowed is and .
+ /// The calling convention for the dynamic method. Must be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A representing the module with which the dynamic method is to be logically associated.
+ /// to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method; otherwise, .
+ /// An element of is or . -or- is a module that provides anonymous hosting for dynamic methods.
+ /// is . -or- is .
+ /// is a combination of flags other than and . -or- is not . -or- is a type for which returns .
+ ///
+ ///
+ /// For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: m is a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ ///
+ /// The dynamic method created with this constructor has access to public and internal (Friend in Visual Basic) members of all the public and internal types contained in module m.
+ /// Skipping the JIT compiler's visibility checks allows the dynamic method to access private and protected members of all other types in the module and in all other assemblies as well. This is useful, for example, when writing code to serialize objects.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
MethodAttributes attributes,
@@ -119,6 +248,39 @@ public DynamicMethod(string name,
false);
}
+ ///
+ /// Creates a dynamic method, specifying the method name, return type, parameter types, and the type with which the dynamic method is logically associated.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A with which the dynamic method is logically associated. The dynamic method has access to all members of the type.
+ /// An element of is or . -or- is an interface, an array, an open generic type, or a type parameter of a generic type or method.
+ /// is . -or- is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ /// For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: owner is in a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ ///
+ ///
+ /// In general, changing the internal fields of classes is not good object-oriented coding practice.
+ ///
+ ///
+ /// This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the CreateDelegate method. For more information, see the class.
+ ///
+ /// The dynamic method created with this constructor has access to all members of the type owner, and to public and internal (Friend in Visual Basic) members of all the other types in the module that contains owner.
+ /// This constructor specifies method attributes Public and Static, calling convention Standard, and does not skip just-in-time (JIT) visibility checks.
+ /// The following code example creates a that is logically associated with a type. This association gives it access to the private members of that type.
+ /// The code example defines a class named Example with a private field, a class named DerivedFromExample that derives from the first class, a delegate type named UseLikeStatic that returns and has parameters of type Example and , and a delegate type named UseLikeInstance that returns and has one parameter of type .
+ /// The example code then creates a that changes the private field of an instance of Example and returns the previous value.
+ /// The example code creates an instance of Example and then creates two delegates. The first is of type UseLikeStatic, which has the same parameters as the dynamic method. The second is of type UseLikeInstance, which lacks the first parameter (of type Example). This delegate is created using the method overload; the second parameter of that method overload is an instance of Example, in this case the instance just created, which is bound to the newly created delegate. Whenever that delegate is invoked, the dynamic method acts on the bound instance of Example.
+ /// The UseLikeStatic delegate is invoked, passing in the instance of Example that is bound to the UseLikeInstance delegate. Then the UseLikeInstance delegate is invoked, so that both delegates act on the same instance of Example. The changes in the values of the internal field are displayed after each call. Finally, a UseLikeInstance delegate is bound to an instance of DerivedFromExample, and the delegate calls are repeated.
+ ///
+ ///
+ ///
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -138,6 +300,26 @@ public DynamicMethod(string name,
false);
}
+ ///
+ /// Creates a dynamic method, specifying the method name, return type, parameter types, the type with which the dynamic method is logically associated, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A with which the dynamic method is logically associated. The dynamic method has access to all members of the type.
+ /// to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method; otherwise, .
+ /// An element of is or . -or- is an interface, an array, an open generic type, or a type parameter of a generic type or method.
+ /// is . -or- is .
+ /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns .
+ ///
+ ///
+ /// For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: owner is in a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ ///
+ /// The dynamic method created with this constructor has access to all members of the type owner, and to public and internal (Friend in Visual Basic) members of all the other types in the module that contains owner. Skipping the JIT compiler's visibility checks allows the dynamic method to access private and protected members of all other types as well. This is useful, for example, when writing code to serialize objects.
+ /// This constructor specifies method attributes Public and Static, and calling convention Standard.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
@@ -158,6 +340,28 @@ public DynamicMethod(string name,
false);
}
+ ///
+ /// Creates a dynamic method, specifying the method name, attributes, calling convention, return type, parameter types, the type with which the dynamic method is logically associated, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
+ ///
+ /// The name of the dynamic method. This can be a zero-length string, but it cannot be .
+ /// A bitwise combination of values that specifies the attributes of the dynamic method. The only combination allowed is and .
+ /// The calling convention for the dynamic method. Must be .
+ /// A object that specifies the return type of the dynamic method, or if the method has no return type.
+ /// An array of objects specifying the types of the parameters of the dynamic method, or if the method has no parameters.
+ /// A with which the dynamic method is logically associated. The dynamic method has access to all members of the type.
+ /// to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method; otherwise, .
+ /// An element of is or . -or- is an interface, an array, an open generic type, or a type parameter of a generic type or method.
+ /// is . -or- is .
+ /// is a combination of flags other than and . -or- is not . -or- is a type for which returns .
+ ///
+ ///
+ /// For backward compatibility, this constructor demands with the ControlEvidence flag if the following conditions are both true: owner is in a module other than the calling module, and the demand for with the MemberAccess flag has failed. If the demand for succeeds, the operation is allowed.
+ ///
+ /// The dynamic method is global to the module that contains the type owner. It has access to all members of the type owner.
+ /// The dynamic method created with this constructor has access to all members of the type owner, and to public and internal (Friend in Visual Basic) members of all the types contained in the module that contains owner. Skipping the JIT compiler's visibility checks allows the dynamic method to access private and protected members of all other types as well. This is useful, for example, when writing code to serialize objects.
+ ///
+ /// How to: Define and Execute Dynamic Methods
+ /// Security Issues in Reflection Emit
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
MethodAttributes attributes,
@@ -297,6 +501,17 @@ private void Init(string name,
// MethodInfo api.
//
+ ///
+ /// Returns the signature of the method, represented as a string.
+ ///
+ /// A string representing the method signature.
+ ///
+ /// The signature includes only types and the method name, if any. Parameter names are not included.
+ /// The following code example displays the method of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override string ToString()
{
var sbName = new ValueStringBuilder(MethodNameBufferSize);
@@ -312,37 +527,250 @@ public override string ToString()
return sbName.ToString();
}
+ ///
+ /// Gets the name of the dynamic method.
+ ///
+ /// The simple name of the method.
+ ///
+ ///
+ /// It is not necessary to name dynamic methods.
+ ///
+ /// The following code example displays the name of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override string Name => _name;
+ ///
+ /// Gets the type that declares the method, which is always for dynamic methods.
+ ///
+ /// Always .
+ ///
+ /// This property always returns null for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type.
+ /// The following code example displays the declaring type of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override Type? DeclaringType => null;
+ ///
+ /// Gets the class that was used in reflection to obtain the method.
+ ///
+ /// Always .
+ ///
+ /// This property always returns null for dynamic methods.
+ /// The following code example displays the reflected type of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override Type? ReflectedType => null;
+ ///
+ /// Gets the module with which the dynamic method is logically associated.
+ ///
+ /// The with which the current dynamic method is associated.
+ ///
+ /// If a module was specified when the dynamic method was created, this property returns that module. If a type was specified as the owner when the dynamic method was created, this property returns the module that contains that type.
+ /// The following code example displays the property of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override Module Module => _module;
// we cannot return a MethodHandle because we cannot track it via GC so this method is off limits
+ ///
+ /// Not supported for dynamic methods.
+ ///
+ /// Not supported for dynamic methods.
+ /// Not allowed for dynamic methods.
public override RuntimeMethodHandle MethodHandle => throw new InvalidOperationException(SR.InvalidOperation_NotAllowedInDynamicMethod);
+ ///
+ /// Gets the attributes specified when the dynamic method was created.
+ ///
+ /// A bitwise combination of the values representing the attributes for the method.
+ ///
+ /// Currently, the method attributes for a dynamic method are always and .
+ /// The following code example displays the method attributes of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override MethodAttributes Attributes => _attributes;
+ ///
+ /// Gets the calling convention specified when the dynamic method was created.
+ ///
+ /// One of the values that indicates the calling convention of the method.
+ ///
+ /// Currently, the calling convention for a dynamic method is always .
+ /// The following code example displays the calling convention of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override CallingConventions CallingConvention => _callingConvention;
+ ///
+ /// Returns the base implementation for the method.
+ ///
+ /// The base implementation of the method.
+ ///
+ /// This method always returns the current DynamicMethod object.
+ ///
public override MethodInfo GetBaseDefinition() => this;
+ ///
+ /// Returns the parameters of the dynamic method.
+ ///
+ /// An array of objects that represent the parameters of the dynamic method.
+ ///
+ /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters.
+ /// The following code example displays the parameters of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override ParameterInfo[] GetParameters() =>
GetParametersAsSpan().ToArray();
internal override ReadOnlySpan GetParametersAsSpan() => LoadParameters();
+ ///
+ /// Returns the implementation flags for the method.
+ ///
+ /// A bitwise combination of values representing the implementation flags for the method.
+ ///
+ /// Currently, method implementation attributes for dynamic methods are always and .
+ ///
public override MethodImplAttributes GetMethodImplementationFlags() =>
MethodImplAttributes.IL | MethodImplAttributes.NoInlining;
+ ///
+ /// Gets a value that indicates whether the current dynamic method is security-critical or security-safe-critical, and therefore can perform critical operations.
+ ///
+ /// if the current dynamic method is security-critical or security-safe-critical; if it is transparent.
+ /// The dynamic method doesn't have a method body.
+ ///
+ ///
+ ///
+ /// The , , and properties report the transparency level of the dynamic method as determined by the common language runtime (CLR). The combinations of these properties are shown in the following table:
+ /// |Security level|IsSecurityCritical|IsSecuritySafeCritical|IsSecurityTransparent|
+ /// |--------------------|------------------------|----------------------------|---------------------------|
+ /// |Critical|true|false|false|
+ /// |Safe critical|true|true|false|
+ /// |Transparent|false|false|true|
+ /// Using these properties is much simpler than examining the security annotations of an assembly and its types, checking the current trust level, and attempting to duplicate the runtime's rules.
+ /// The transparency of a dynamic method depends on the module it is associated with. If the dynamic method is associated with a type rather than a module, its transparency depends on the module that contains the type. Dynamic methods do not have security annotations, so they are assigned the default transparency for the associated module.
+ /// - Anonymously hosted dynamic methods are always transparent, because the system-provided module that contains them is transparent.
+ /// - The transparency of a dynamic method that is associated with a trusted assembly (that is, a strong-named assembly that is installed in the global assembly cache) is described in the following table.
+ /// |Assembly annotation|Level 1 transparency|Level 2 transparency|
+ /// |-------------------------|--------------------------|--------------------------|
+ /// |Fully transparent|Transparent|Transparent|
+ /// |Fully critical|Critical|Critical|
+ /// |Mixed transparency|Transparent|Transparent|
+ /// |Security-agnostic|Safe-critical|Critical|
+ /// For example, if you associate a dynamic method with a type that is in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and cannot execute critical code. For information about transparency levels, see [Security-Transparent Code, Level 1](/dotnet/framework/misc/security-transparent-code-level-1) and [Security-Transparent Code, Level 2](/dotnet/framework/misc/security-transparent-code-level-2).
+ /// > Associating a dynamic method with a module in a trusted level 1 assembly that is security-agnostic, such as System.dll, does not permit elevation of trust. If the grant set of the code that calls the dynamic method does not include the grant set of System.dll (that is, full trust), is thrown when the dynamic method is called.
+ /// - The transparency of a dynamic method that is associated with a partially trusted assembly depends on how the assembly is loaded. If the assembly is loaded with partial trust (for example, into a sandboxed application domain), the runtime ignores the security annotations of the assembly. The assembly and all its types and members, including dynamic methods, are treated as transparent. The runtime pays attention to security annotations only if the partial-trust assembly is loaded with full trust (for example, into the default application domain of a desktop application). In that case, the runtime assigns the dynamic method the default transparency for methods according to the assembly's annotations.
+ /// For more information about reflection emit and transparency, see [Security Issues in Reflection Emit](/dotnet/framework/reflection-and-codedom/security-issues-in-reflection-emit). For information about transparency, see [Security Changes](/dotnet/framework/security/security-changes).
+ ///
+ /// Security Issues in Reflection Emit
+ /// Security Considerations for Reflection
+ /// Security Changes in the .NET Framework Version 4.0
+ /// Security-Transparent Code, Level 1
+ /// Security-Transparent Code, Level 2
public override bool IsSecurityCritical => true;
+ ///
+ /// Gets a value that indicates whether the current dynamic method is security-safe-critical at the current trust level; that is, whether it can perform critical operations and can be accessed by transparent code.
+ ///
+ /// if the dynamic method is security-safe-critical at the current trust level; if it is security-critical or transparent.
+ /// The dynamic method doesn't have a method body.
+ ///
+ ///
+ ///
+ /// The , , and properties report the transparency level of the dynamic method as determined by the common language runtime (CLR). The combinations of these properties are shown in the following table:
+ /// |Security level|IsSecurityCritical|IsSecuritySafeCritical|IsSecurityTransparent|
+ /// |--------------------|------------------------|----------------------------|---------------------------|
+ /// |Critical|true|false|false|
+ /// |Safe critical|true|true|false|
+ /// |Transparent|false|false|true|
+ /// Using these properties is much simpler than examining the security annotations of an assembly and its types, checking the current trust level, and attempting to duplicate the runtime's rules.
+ /// The transparency of a dynamic method depends on the module it is associated with. If the dynamic method is associated with a type rather than a module, its transparency depends on the module that contains the type. Dynamic methods do not have security annotations, so they are assigned the default transparency for the associated module.
+ /// - Anonymously hosted dynamic methods are always transparent, because the system-provided module that contains them is transparent.
+ /// - The transparency of a dynamic method that is associated with a trusted assembly (that is, a strong-named assembly that is installed in the global assembly cache) is described in the following table.
+ /// |Assembly annotation|Level 1 transparency|Level 2 transparency|
+ /// |-------------------------|--------------------------|--------------------------|
+ /// |Fully transparent|Transparent|Transparent|
+ /// |Fully critical|Critical|Critical|
+ /// |Mixed transparency|Transparent|Transparent|
+ /// |Security-agnostic|Safe-critical|Critical|
+ /// For example, if you associate a dynamic method with a type that is in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and cannot execute critical code. For information about transparency levels, see [Security-Transparent Code, Level 1](/dotnet/framework/misc/security-transparent-code-level-1) and [Security-Transparent Code, Level 2](/dotnet/framework/misc/security-transparent-code-level-2).
+ /// > Associating a dynamic method with a module in a trusted level 1 assembly that is security-agnostic, such as System.dll, does not permit elevation of trust. If the grant set of the code that calls the dynamic method does not include the grant set of System.dll (that is, full trust), is thrown when the dynamic method is called.
+ /// - The transparency of a dynamic method that is associated with a partially trusted assembly depends on how the assembly is loaded. If the assembly is loaded with partial trust (for example, into a sandboxed application domain), the runtime ignores the security annotations of the assembly. The assembly and all its types and members, including dynamic methods, are treated as transparent. The runtime pays attention to security annotations only if the partial-trust assembly is loaded with full trust (for example, into the default application domain of a desktop application). In that case, the runtime assigns the dynamic method the default transparency for methods according to the assembly's annotations.
+ /// For more information about reflection emit and transparency, see [Security Issues in Reflection Emit](/dotnet/framework/reflection-and-codedom/security-issues-in-reflection-emit). For information about transparency, see [Security Changes](/dotnet/framework/security/security-changes).
+ ///
+ /// Security Issues in Reflection Emit
+ /// Security Considerations for Reflection
+ /// Security Changes in the .NET Framework Version 4.0
+ /// Security-Transparent Code, Level 1
+ /// Security-Transparent Code, Level 2
public override bool IsSecuritySafeCritical => false;
+ ///
+ /// Gets a value that indicates whether the current dynamic method is transparent at the current trust level, and therefore cannot perform critical operations.
+ ///
+ /// if the dynamic method is security-transparent at the current trust level; otherwise, .
+ /// The dynamic method doesn't have a method body.
+ ///
+ ///
+ ///
+ /// The , , and properties report the transparency level of the dynamic method as determined by the common language runtime (CLR). The combinations of these properties are shown in the following table:
+ /// |Security level|IsSecurityCritical|IsSecuritySafeCritical|IsSecurityTransparent|
+ /// |--------------------|------------------------|----------------------------|---------------------------|
+ /// |Critical|true|false|false|
+ /// |Safe critical|true|true|false|
+ /// |Transparent|false|false|true|
+ /// Using these properties is much simpler than examining the security annotations of an assembly and its types, checking the current trust level, and attempting to duplicate the runtime's rules.
+ /// The transparency of a dynamic method depends on the module it is associated with. If the dynamic method is associated with a type rather than a module, its transparency depends on the module that contains the type. Dynamic methods do not have security annotations, so they are assigned the default transparency for the associated module.
+ /// - Anonymously hosted dynamic methods are always transparent, because the system-provided module that contains them is transparent.
+ /// - The transparency of a dynamic method that is associated with a trusted assembly (that is, a strong-named assembly that is installed in the global assembly cache) is described in the following table.
+ /// |Assembly annotation|Level 1 transparency|Level 2 transparency|
+ /// |-------------------------|--------------------------|--------------------------|
+ /// |Fully transparent|Transparent|Transparent|
+ /// |Fully critical|Critical|Critical|
+ /// |Mixed transparency|Transparent|Transparent|
+ /// |Security-agnostic|Safe-critical|Critical|
+ /// For example, if you associate a dynamic method with a type that is in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and cannot execute critical code. For information about transparency levels, see [Security-Transparent Code, Level 1](/dotnet/framework/misc/security-transparent-code-level-1) and [Security-Transparent Code, Level 2](/dotnet/framework/misc/security-transparent-code-level-2).
+ /// > Associating a dynamic method with a module in a trusted level 1 assembly that is security-agnostic, such as System.dll, does not permit elevation of trust. If the grant set of the code that calls the dynamic method does not include the grant set of System.dll (that is, full trust), is thrown when the dynamic method is called.
+ /// - The transparency of a dynamic method that is associated with a partially trusted assembly depends on how the assembly is loaded. If the assembly is loaded with partial trust (for example, into a sandboxed application domain), the runtime ignores the security annotations of the assembly. The assembly and all its types and members, including dynamic methods, are treated as transparent. The runtime pays attention to security annotations only if the partial-trust assembly is loaded with full trust (for example, into the default application domain of a desktop application). In that case, the runtime assigns the dynamic method the default transparency for methods according to the assembly's annotations.
+ /// For more information about reflection emit and transparency, see [Security Issues in Reflection Emit](/dotnet/framework/reflection-and-codedom/security-issues-in-reflection-emit). For information about transparency, see [Security Changes](/dotnet/framework/security/security-changes).
+ ///
+ /// Security Issues in Reflection Emit
+ /// Security Considerations for Reflection
+ /// Security Changes in the .NET Framework Version 4.0
+ /// Security-Transparent Code, Level 1
+ /// Security-Transparent Code, Level 2
public override bool IsSecurityTransparent => false;
+ ///
+ /// Returns the custom attributes of the specified type that have been applied to the method.
+ ///
+ /// A representing the type of custom attribute to return.
+ /// to search the method's inheritance chain to find the custom attributes; to check only the current method.
+ /// An array of objects representing the attributes of the method that are of type or derive from type .
+ /// is .
+ ///
+ ///
+ /// Custom attributes are not currently supported on dynamic methods. The only attribute returned is ; you can get the method implementation flags more easily using the method.
+ ///
+ /// For dynamic methods, specifying true for inherit has no effect, because the method is not declared in a type.
+ ///
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
@@ -359,12 +787,35 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
return result;
}
+ ///
+ /// Returns all the custom attributes defined for the method.
+ ///
+ /// to search the method's inheritance chain to find the custom attributes; to check only the current method.
+ /// An array of objects representing all the custom attributes of the method.
+ ///
+ ///
+ /// Custom attributes are not currently supported on dynamic methods. The only attribute returned is ; you can get the method implementation flags more easily using the method.
+ ///
+ /// For dynamic methods, specifying true for inherit has no effect, because the method is not declared in a type.
+ ///
public override object[] GetCustomAttributes(bool inherit)
{
// support for MethodImplAttribute PCA
return [new MethodImplAttribute((MethodImplOptions)GetMethodImplementationFlags())];
}
+ ///
+ /// Indicates whether the specified custom attribute type is defined.
+ ///
+ /// A representing the type of custom attribute to search for.
+ /// to search the method's inheritance chain to find the custom attributes; to check only the current method.
+ /// if the specified custom attribute type is defined; otherwise, .
+ ///
+ ///
+ /// Custom attributes are not currently supported on dynamic methods.
+ ///
+ /// For dynamic methods, specifying true for inherit has no effect. Dynamic methods have no inheritance chain.
+ ///
public override bool IsDefined(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
@@ -372,16 +823,61 @@ public override bool IsDefined(Type attributeType, bool inherit)
return attributeType.IsAssignableFrom(typeof(MethodImplAttribute));
}
+ ///
+ /// Gets the type of return value for the dynamic method.
+ ///
+ /// A representing the type of the return value of the current method; if the method has no return type.
+ ///
+ /// If null was specified for the return type when the dynamic method was created, this property returns Void.
+ /// The following code example displays the return type of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override Type ReturnType => _returnType;
+ ///
+ /// Gets the return parameter of the dynamic method.
+ ///
+ /// Always .
+ ///
+ /// This property always returns null for dynamic methods.
+ ///
public override ParameterInfo ReturnParameter => new RuntimeParameterInfo(this, null, _returnType, -1);
+ ///
+ /// Gets the custom attributes of the return type for the dynamic method.
+ ///
+ /// An representing the custom attributes of the return type for the dynamic method.
+ ///
+ /// Custom attributes are not supported on the return type of a dynamic method, so the array of custom attributes returned by the method is always empty.
+ /// The following code example shows how to display the custom attributes of the return type of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public override ICustomAttributeProvider ReturnTypeCustomAttributes => new EmptyCAHolder();
//
// DynamicMethod specific methods
//
+ ///
+ /// Defines a parameter of the dynamic method.
+ ///
+ /// The position of the parameter in the parameter list. Parameters are indexed beginning with the number 1 for the first parameter.
+ /// A bitwise combination of values that specifies the attributes of the parameter.
+ /// The name of the parameter. The name can be a zero-length string.
+ /// Always returns .
+ /// The method has no parameters. -or- is less than 0. -or- is greater than the number of the method's parameters.
+ ///
+ /// If position is 0, the method refers to the return value. Setting parameter information has no effect on the return value.
+ /// If the dynamic method has already been completed, by calling the or method, the method has no effect. No exception is thrown.
+ /// The following code example shows how to define parameter information for a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public ParameterBuilder? DefineParameter(int position, ParameterAttributes attributes, string? parameterName)
{
if (position < 0 || position > _parameterTypes.Length)
@@ -397,11 +893,37 @@ public override bool IsDefined(Type attributeType, bool inherit)
return null;
}
+ ///
+ /// Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream size of 64 bytes.
+ ///
+ /// An object for the method.
+ ///
+ ///
+ /// There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for .
+ ///
+ /// After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown.
+ /// The following code example creates a dynamic method that takes two parameters. The example emits a simple function body that prints the first parameter to the console, and the example uses the second parameter as the return value of the method. The example completes the method by creating a delegate, invokes the delegate with different parameters, and finally invokes the dynamic method using the method.
+ ///
+ ///
+ ///
+ ///
+ /// How to: Define and Execute Dynamic Methods
public ILGenerator GetILGenerator()
{
return GetILGenerator(64);
}
+ ///
+ /// Gets or sets a value indicating whether the local variables in the method are zero-initialized.
+ ///
+ /// if the local variables in the method are zero-initialized; otherwise, . The default is .
+ ///
+ /// If this property is set to true, the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to false, local variables are not initialized and the generated code is unverifiable.
+ /// The following code example displays the property of a dynamic method. This code example is part of a larger example provided for the class.
+ ///
+ ///
+ ///
+ ///
public bool InitLocals
{
get => _initLocals;