From b02414b0a6ebb2c8276bbbcb0cf9755ed8d9563b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 15:47:36 +0000 Subject: [PATCH 1/8] Initial plan From f1fa223f1ac9937e1fef660336dc863add59ac66 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 15:56:50 +0000 Subject: [PATCH 2/8] Add XML documentation to DynamicMethod class Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../Reflection/Emit/DynamicMethod.CoreCLR.cs | 48 ++++ .../System/Reflection/Emit/DynamicMethod.cs | 264 ++++++++++++++++++ .../Reflection/Emit/DynamicMethod.Mono.cs | 48 ++++ 3 files changed, 360 insertions(+) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs index abdb8be14b27a3..44aaba75179196 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs @@ -41,9 +41,30 @@ public sealed partial class DynamicMethod : MethodInfo // Delegate and method creation // + /// + /// Completes the dynamic method and creates a delegate that can be used to execute it. + /// + /// A delegate type whose signature matches that of the dynamic method. + /// A delegate of the specified type, which can be used to execute the dynamic method. + /// The dynamic method has no method body. + /// has the wrong number of parameters or the wrong parameter types. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public sealed override Delegate CreateDelegate(Type delegateType) => CreateDelegate(delegateType, target: null); + /// + /// Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to. + /// + /// A delegate type whose signature matches that of the dynamic method, minus the first parameter. + /// An object the delegate is bound to. Must be of the same type as the first parameter of the dynamic method. + /// A delegate of the specified type, which can be used to execute the dynamic method with the specified target object. + /// The dynamic method has no method body. + /// has the wrong number of parameters or the wrong parameter types. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public sealed override Delegate CreateDelegate(Type delegateType, object? target) { if (_restrictedSkipVisibility) @@ -114,6 +135,21 @@ Signature LazyCreateSignature() } } + /// + /// Invokes the dynamic method using the specified parameters, under the constraints of the specified binder, with the specified culture information. + /// + /// This parameter is ignored for dynamic methods, because they are static. Specify . + /// A bitwise combination of values. + /// A object that enables the binding, coercion of argument types, invocation of members, and retrieval of objects through reflection. If is , the default binder is used. + /// An argument list. This is an array of arguments with the same number, order, and type as the parameters of the method to be invoked. If there are no parameters this parameter should be . + /// An instance of used to govern the coercion of types. If this is , the for the current thread is used. + /// An containing the return value of the invoked method. + /// The number of elements in does not match the number of parameters in the dynamic method. + /// The dynamic method contains unverifiable code. + /// The dynamic method has no method body. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs) @@ -143,6 +179,10 @@ Signature LazyCreateSignature() return retValue; } + /// + /// Returns a object that can be used to generate a method body from metadata tokens, scopes, and Microsoft intermediate language (MSIL) streams. + /// + /// A object that can be used to generate a method body from metadata tokens, scopes, and MSIL streams. public DynamicILInfo GetDynamicILInfo() { if (_dynamicILInfo == null) @@ -154,6 +194,14 @@ public DynamicILInfo GetDynamicILInfo() return _dynamicILInfo; } + /// + /// Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size. + /// + /// The size of the MSIL stream, in bytes. + /// An object for the method. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public ILGenerator GetILGenerator(int streamSize) { if (_ilGenerator == null) 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..4d768eea558b4e 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,12 @@ 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. + /// public sealed partial class DynamicMethod : MethodInfo { // The context when the method was created. We use this to do the RestrictedMemberAccess checks. @@ -25,6 +31,18 @@ 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 . + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -41,6 +59,19 @@ 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 . + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -58,6 +89,23 @@ 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 . + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -77,6 +125,24 @@ 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; otherwise, . + /// 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 more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -97,6 +163,30 @@ 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- + /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns . + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, MethodAttributes attributes, @@ -119,6 +209,23 @@ 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 more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -138,6 +245,24 @@ 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 more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, Type? returnType, @@ -158,6 +283,30 @@ 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- + /// .NET Framework and .NET Core versions older than 2.1: is a type for which returns . + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// [RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")] public DynamicMethod(string name, MethodAttributes attributes, @@ -297,6 +446,10 @@ private void Init(string name, // MethodInfo api. // + /// + /// Returns a string representation of the dynamic method. + /// + /// A string representation of the dynamic method, showing the return type, name, and parameter types. public override string ToString() { var sbName = new ValueStringBuilder(MethodNameBufferSize); @@ -312,37 +465,101 @@ public override string ToString() return sbName.ToString(); } + /// + /// Gets the name of the dynamic method. + /// + /// The name of the dynamic method. public override string Name => _name; + /// + /// Gets the type that declares the dynamic method. + /// + /// Always for dynamic methods. public override Type? DeclaringType => null; + /// + /// Gets the class object that was used to obtain the instance of the dynamic method. + /// + /// Always for dynamic methods. public override Type? ReflectedType => null; + /// + /// Gets the module associated with the dynamic method. + /// + /// The associated with the dynamic method. 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. + /// Always thrown. The of a dynamic method is not supported. 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. 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. public override CallingConventions CallingConvention => _callingConvention; + /// + /// Returns the base definition for the dynamic method. + /// + /// Always returns this dynamic method. public override MethodInfo GetBaseDefinition() => this; + /// + /// Returns an array of objects representing the parameters of the dynamic method. + /// + /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. 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. public override MethodImplAttributes GetMethodImplementationFlags() => MethodImplAttributes.IL | MethodImplAttributes.NoInlining; + /// + /// Gets a value that indicates whether the dynamic method is security-critical. + /// + /// for all dynamic methods. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public override bool IsSecurityCritical => true; + /// + /// Gets a value that indicates whether the dynamic method is security-safe-critical. + /// + /// for all dynamic methods. public override bool IsSecuritySafeCritical => false; + /// + /// Gets a value that indicates whether the dynamic method is security-transparent. + /// + /// for all dynamic methods. public override bool IsSecurityTransparent => false; + /// + /// Returns an array of all custom attributes defined on the dynamic method. + /// + /// The type of attribute to search for. Only attributes that are assignable to this type are returned. + /// This parameter is ignored for dynamic methods, because they do not support inheritance. + /// An array of custom attributes defined on the dynamic method. If no attributes of the specified type are defined, an empty array is returned. + /// is not a . + /// is . public override object[] GetCustomAttributes(Type attributeType, bool inherit) { ArgumentNullException.ThrowIfNull(attributeType); @@ -359,12 +576,24 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) return result; } + /// + /// Returns an array of all custom attributes defined on the dynamic method. + /// + /// This parameter is ignored for dynamic methods, because they do not support inheritance. + /// An array of all custom attributes defined on the dynamic method. public override object[] GetCustomAttributes(bool inherit) { // support for MethodImplAttribute PCA return [new MethodImplAttribute((MethodImplOptions)GetMethodImplementationFlags())]; } + /// + /// Indicates whether one or more attributes of the specified type or of its derived types is applied to this method. + /// + /// The type of custom attribute to search for. The search includes derived types. + /// This parameter is ignored for dynamic methods, because they do not support inheritance. + /// if one or more instances of or any of its derived types is applied to this method; otherwise, . + /// is . public override bool IsDefined(Type attributeType, bool inherit) { ArgumentNullException.ThrowIfNull(attributeType); @@ -372,16 +601,40 @@ public override bool IsDefined(Type attributeType, bool inherit) return attributeType.IsAssignableFrom(typeof(MethodImplAttribute)); } + /// + /// Gets the return type of the dynamic method. + /// + /// A representing the return type of the dynamic method; or if the method has no return type. public override Type ReturnType => _returnType; + /// + /// Gets the return parameter of the dynamic method. + /// + /// A object that represents the return parameter of the dynamic method. 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. 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 parameters of the dynamic method. public ParameterBuilder? DefineParameter(int position, ParameterAttributes attributes, string? parameterName) { if (position < 0 || position > _parameterTypes.Length) @@ -397,11 +650,22 @@ 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. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// 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 . public bool InitLocals { get => _initLocals; diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs index 3ec41bb31fbda9..ecdfe8ef8976d1 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs @@ -36,6 +36,16 @@ public sealed partial class DynamicMethod : MethodInfo private object? _methodHandle; // unused + /// + /// Completes the dynamic method and creates a delegate that can be used to execute it. + /// + /// A delegate type whose signature matches that of the dynamic method. + /// A delegate of the specified type, which can be used to execute the dynamic method. + /// The dynamic method has no method body. + /// has the wrong number of parameters or the wrong parameter types. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public sealed override Delegate CreateDelegate(Type delegateType) { ArgumentNullException.ThrowIfNull(delegateType); @@ -47,6 +57,17 @@ public sealed override Delegate CreateDelegate(Type delegateType) return _deleg = Delegate.CreateDelegate(delegateType, null, this); } + /// + /// Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to. + /// + /// A delegate type whose signature matches that of the dynamic method, minus the first parameter. + /// An object the delegate is bound to. Must be of the same type as the first parameter of the dynamic method. + /// A delegate of the specified type, which can be used to execute the dynamic method with the specified target object. + /// The dynamic method has no method body. + /// has the wrong number of parameters or the wrong parameter types. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public sealed override Delegate CreateDelegate(Type delegateType, object? target) { ArgumentNullException.ThrowIfNull(delegateType); @@ -57,8 +78,20 @@ public sealed override Delegate CreateDelegate(Type delegateType, object? target return Delegate.CreateDelegate(delegateType, target, this); } + /// + /// Returns a object that can be used to generate a method body from metadata tokens, scopes, and Microsoft intermediate language (MSIL) streams. + /// + /// A object that can be used to generate a method body from metadata tokens, scopes, and MSIL streams. public DynamicILInfo GetDynamicILInfo() => _dynamicILInfo ??= new DynamicILInfo(this); + /// + /// Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size. + /// + /// The size of the MSIL stream, in bytes. + /// An object for the method. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public ILGenerator GetILGenerator(int streamSize) => GetILGeneratorInternal(streamSize); internal RuntimeILGenerator GetRuntimeILGenerator() => GetILGeneratorInternal(64); @@ -66,6 +99,21 @@ public sealed override Delegate CreateDelegate(Type delegateType, object? target private RuntimeILGenerator GetILGeneratorInternal(int streamSize) => _ilGenerator ??= new RuntimeILGenerator(Module, new DynamicMethodTokenGenerator(this), streamSize); + /// + /// Invokes the dynamic method using the specified parameters, under the constraints of the specified binder, with the specified culture information. + /// + /// This parameter is ignored for dynamic methods, because they are static. Specify . + /// A bitwise combination of values. + /// A object that enables the binding, coercion of argument types, invocation of members, and retrieval of objects through reflection. If is , the default binder is used. + /// An argument list. This is an array of arguments with the same number, order, and type as the parameters of the method to be invoked. If there are no parameters this parameter should be . + /// An instance of used to govern the coercion of types. If this is , the for the current thread is used. + /// An containing the return value of the invoked method. + /// The number of elements in does not match the number of parameters in the dynamic method. + /// The dynamic method contains unverifiable code. + /// The dynamic method has no method body. + /// + /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs) From 2f4fe143d7da2624885af6aa92d900c24c30f592 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 17:03:11 +0000 Subject: [PATCH 3/8] Address review feedback: add missing remarks and fix supplemental links Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../Reflection/Emit/DynamicMethod.CoreCLR.cs | 11 ++- .../System/Reflection/Emit/DynamicMethod.cs | 83 +++++++++++++++++-- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs index 44aaba75179196..6df897b2c78e51 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs @@ -49,7 +49,7 @@ public sealed partial class DynamicMethod : MethodInfo /// The dynamic method has no method body. /// has the wrong number of parameters or the wrong parameter types. /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.CreateDelegate. /// public sealed override Delegate CreateDelegate(Type delegateType) => CreateDelegate(delegateType, target: null); @@ -63,7 +63,7 @@ public sealed override Delegate CreateDelegate(Type delegateType) => /// The dynamic method has no method body. /// has the wrong number of parameters or the wrong parameter types. /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.CreateDelegate. /// public sealed override Delegate CreateDelegate(Type delegateType, object? target) { @@ -148,7 +148,7 @@ Signature LazyCreateSignature() /// The dynamic method contains unverifiable code. /// The dynamic method has no method body. /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.Invoke. /// public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { @@ -183,6 +183,9 @@ Signature LazyCreateSignature() /// Returns a object that can be used to generate a method body from metadata tokens, scopes, and Microsoft intermediate language (MSIL) streams. /// /// A object that can be used to generate a method body from metadata tokens, scopes, and MSIL streams. + /// + /// The class is provided to support unmanaged code generation. + /// public DynamicILInfo GetDynamicILInfo() { if (_dynamicILInfo == null) @@ -200,7 +203,7 @@ public DynamicILInfo GetDynamicILInfo() /// The size of the MSIL stream, in bytes. /// An object for the method. /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.GetILGenerator. /// public ILGenerator GetILGenerator(int streamSize) { 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 4d768eea558b4e..2541ea49d1b736 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 @@ -450,6 +450,9 @@ private void Init(string name, /// Returns a string representation of the dynamic method. /// /// A string representation of the dynamic method, showing the return type, name, and parameter types. + /// + /// The signature includes only types and the method name, if any. Parameter names are not included. + /// public override string ToString() { var sbName = new ValueStringBuilder(MethodNameBufferSize); @@ -469,24 +472,36 @@ public override string ToString() /// Gets the name of the dynamic method. /// /// The name of the dynamic method. + /// + /// It is not necessary to name dynamic methods. + /// public override string Name => _name; /// /// Gets the type that declares the dynamic method. /// /// Always for dynamic methods. + /// + /// This property always returns for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type. + /// public override Type? DeclaringType => null; /// /// Gets the class object that was used to obtain the instance of the dynamic method. /// /// Always for dynamic methods. + /// + /// This property always returns for dynamic methods. + /// public override Type? ReflectedType => null; /// /// Gets the module associated with the dynamic method. /// /// The associated with the dynamic method. + /// + /// 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. + /// public override Module Module => _module; // we cannot return a MethodHandle because we cannot track it via GC so this method is off limits @@ -501,24 +516,36 @@ public override string ToString() /// 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 . + /// 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 . + /// public override CallingConventions CallingConvention => _callingConvention; /// /// Returns the base definition for the dynamic method. /// /// Always returns this dynamic method. + /// + /// This method always returns the current object. + /// public override MethodInfo GetBaseDefinition() => this; /// /// Returns an array of objects representing the parameters of the dynamic method. /// /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. + /// + /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters. + /// public override ParameterInfo[] GetParameters() => GetParametersAsSpan().ToArray(); @@ -528,28 +555,46 @@ public override ParameterInfo[] GetParameters() => /// 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 dynamic method is security-critical. /// - /// for all dynamic methods. + /// + /// .NET (Core): for all dynamic methods. + /// .NET Framework: if the current dynamic method is security-critical or security-safe-critical; if it is transparent. + /// /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.IsSecurityCritical. /// public override bool IsSecurityCritical => true; /// /// Gets a value that indicates whether the dynamic method is security-safe-critical. /// - /// for all dynamic methods. + /// + /// .NET (Core): for all dynamic methods. + /// .NET Framework: if the dynamic method is safe-critical; if it is critical or transparent. + /// + /// + /// For .NET Framework remarks about security transparency, see the property. + /// public override bool IsSecuritySafeCritical => false; /// /// Gets a value that indicates whether the dynamic method is security-transparent. /// - /// for all dynamic methods. + /// + /// .NET (Core): for all dynamic methods. + /// .NET Framework: if the dynamic method is transparent; otherwise, . + /// + /// + /// For .NET Framework remarks about security transparency, see the property. + /// public override bool IsSecurityTransparent => false; /// @@ -560,6 +605,10 @@ public override MethodImplAttributes GetMethodImplementationFlags() => /// An array of custom attributes defined on the dynamic method. If no attributes of the specified type are defined, an empty array is returned. /// is not a . /// is . + /// + /// For dynamic methods, specifying for has no effect, because the method is not declared in a type. + /// 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. + /// public override object[] GetCustomAttributes(Type attributeType, bool inherit) { ArgumentNullException.ThrowIfNull(attributeType); @@ -581,6 +630,10 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) /// /// This parameter is ignored for dynamic methods, because they do not support inheritance. /// An array of all custom attributes defined on the dynamic method. + /// + /// For dynamic methods, specifying for has no effect, because the method is not declared in a type. + /// 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. + /// public override object[] GetCustomAttributes(bool inherit) { // support for MethodImplAttribute PCA @@ -594,6 +647,10 @@ public override object[] GetCustomAttributes(bool inherit) /// This parameter is ignored for dynamic methods, because they do not support inheritance. /// if one or more instances of or any of its derived types is applied to this method; otherwise, . /// is . + /// + /// For dynamic methods, specifying for has no effect. Dynamic methods have no inheritance chain. + /// Custom attributes are not currently supported on dynamic methods. + /// public override bool IsDefined(Type attributeType, bool inherit) { ArgumentNullException.ThrowIfNull(attributeType); @@ -605,18 +662,27 @@ public override bool IsDefined(Type attributeType, bool inherit) /// Gets the return type of the dynamic method. /// /// A representing the return type of the dynamic method; or if the method has no return type. + /// + /// If was specified for the return type when the dynamic method was created, this property returns . + /// public override Type ReturnType => _returnType; /// /// Gets the return parameter of the dynamic method. /// /// A object that represents the return parameter of the dynamic method. + /// + /// This property always returns 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. + /// public override ICustomAttributeProvider ReturnTypeCustomAttributes => new EmptyCAHolder(); // @@ -635,6 +701,10 @@ public override bool IsDefined(Type attributeType, bool inherit) /// is less than 0. /// -or- /// is greater than the number of parameters of the dynamic method. + /// + /// If 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. + /// public ParameterBuilder? DefineParameter(int position, ParameterAttributes attributes, string? parameterName) { if (position < 0 || position > _parameterTypes.Length) @@ -655,7 +725,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// An object for the method. /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. + /// For more information about this API, see Supplemental API remarks for DynamicMethod.GetILGenerator. /// public ILGenerator GetILGenerator() { @@ -666,6 +736,9 @@ public ILGenerator GetILGenerator() /// 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 , the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to , local variables are not initialized and the generated code is unverifiable. + /// public bool InitLocals { get => _initLocals; From 69754d84a2449b8971a35afee296d1a833183a28 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 17:05:45 +0000 Subject: [PATCH 4/8] Remove duplicate documentation from Mono file Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../Reflection/Emit/DynamicMethod.Mono.cs | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs index ecdfe8ef8976d1..3ec41bb31fbda9 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Mono.cs @@ -36,16 +36,6 @@ public sealed partial class DynamicMethod : MethodInfo private object? _methodHandle; // unused - /// - /// Completes the dynamic method and creates a delegate that can be used to execute it. - /// - /// A delegate type whose signature matches that of the dynamic method. - /// A delegate of the specified type, which can be used to execute the dynamic method. - /// The dynamic method has no method body. - /// has the wrong number of parameters or the wrong parameter types. - /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. - /// public sealed override Delegate CreateDelegate(Type delegateType) { ArgumentNullException.ThrowIfNull(delegateType); @@ -57,17 +47,6 @@ public sealed override Delegate CreateDelegate(Type delegateType) return _deleg = Delegate.CreateDelegate(delegateType, null, this); } - /// - /// Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to. - /// - /// A delegate type whose signature matches that of the dynamic method, minus the first parameter. - /// An object the delegate is bound to. Must be of the same type as the first parameter of the dynamic method. - /// A delegate of the specified type, which can be used to execute the dynamic method with the specified target object. - /// The dynamic method has no method body. - /// has the wrong number of parameters or the wrong parameter types. - /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. - /// public sealed override Delegate CreateDelegate(Type delegateType, object? target) { ArgumentNullException.ThrowIfNull(delegateType); @@ -78,20 +57,8 @@ public sealed override Delegate CreateDelegate(Type delegateType, object? target return Delegate.CreateDelegate(delegateType, target, this); } - /// - /// Returns a object that can be used to generate a method body from metadata tokens, scopes, and Microsoft intermediate language (MSIL) streams. - /// - /// A object that can be used to generate a method body from metadata tokens, scopes, and MSIL streams. public DynamicILInfo GetDynamicILInfo() => _dynamicILInfo ??= new DynamicILInfo(this); - /// - /// Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL stream size. - /// - /// The size of the MSIL stream, in bytes. - /// An object for the method. - /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. - /// public ILGenerator GetILGenerator(int streamSize) => GetILGeneratorInternal(streamSize); internal RuntimeILGenerator GetRuntimeILGenerator() => GetILGeneratorInternal(64); @@ -99,21 +66,6 @@ public sealed override Delegate CreateDelegate(Type delegateType, object? target private RuntimeILGenerator GetILGeneratorInternal(int streamSize) => _ilGenerator ??= new RuntimeILGenerator(Module, new DynamicMethodTokenGenerator(this), streamSize); - /// - /// Invokes the dynamic method using the specified parameters, under the constraints of the specified binder, with the specified culture information. - /// - /// This parameter is ignored for dynamic methods, because they are static. Specify . - /// A bitwise combination of values. - /// A object that enables the binding, coercion of argument types, invocation of members, and retrieval of objects through reflection. If is , the default binder is used. - /// An argument list. This is an array of arguments with the same number, order, and type as the parameters of the method to be invoked. If there are no parameters this parameter should be . - /// An instance of used to govern the coercion of types. If this is , the for the current thread is used. - /// An containing the return value of the invoked method. - /// The number of elements in does not match the number of parameters in the dynamic method. - /// The dynamic method contains unverifiable code. - /// The dynamic method has no method body. - /// - /// For more information about this API, see Supplemental API remarks for DynamicMethod. - /// public override object? Invoke(object? obj, BindingFlags invokeAttr, Binder? binder, object?[]? parameters, CultureInfo? culture) { if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs) From ac08171b5691de70a128c8a42bbde1db331d9d79 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 19:47:18 +0000 Subject: [PATCH 5/8] Add example sections to DynamicMethod documentation Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../System/Reflection/Emit/DynamicMethod.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 2541ea49d1b736..4da5637a8f4822 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 @@ -450,6 +450,10 @@ private void Init(string name, /// Returns a string representation of the dynamic method. /// /// A string representation of the dynamic method, showing the return type, name, and parameter types. + /// + /// + /// + /// /// /// The signature includes only types and the method name, if any. Parameter names are not included. /// @@ -472,6 +476,10 @@ public override string ToString() /// Gets the name of the dynamic method. /// /// The name of the dynamic method. + /// + /// + /// + /// /// /// It is not necessary to name dynamic methods. /// @@ -481,6 +489,10 @@ public override string ToString() /// Gets the type that declares the dynamic method. /// /// Always for dynamic methods. + /// + /// + /// + /// /// /// This property always returns for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type. /// @@ -490,6 +502,10 @@ public override string ToString() /// Gets the class object that was used to obtain the instance of the dynamic method. /// /// Always for dynamic methods. + /// + /// + /// + /// /// /// This property always returns for dynamic methods. /// @@ -499,6 +515,10 @@ public override string ToString() /// Gets the module associated with the dynamic method. /// /// The associated with the dynamic method. + /// + /// + /// + /// /// /// 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. /// @@ -516,6 +536,10 @@ public override string ToString() /// 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 . /// @@ -525,6 +549,10 @@ public override string ToString() /// 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 . /// @@ -543,6 +571,10 @@ public override string ToString() /// Returns an array of objects representing the parameters of the dynamic method. /// /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. + /// + /// + /// + /// /// /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters. /// @@ -662,6 +694,10 @@ public override bool IsDefined(Type attributeType, bool inherit) /// Gets the return type of the dynamic method. /// /// A representing the return type of the dynamic method; or if the method has no return type. + /// + /// + /// + /// /// /// If was specified for the return type when the dynamic method was created, this property returns . /// @@ -680,6 +716,10 @@ public override bool IsDefined(Type attributeType, bool inherit) /// 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. /// @@ -701,6 +741,10 @@ public override bool IsDefined(Type attributeType, bool inherit) /// is less than 0. /// -or- /// is greater than the number of parameters of the dynamic method. + /// + /// + /// + /// /// /// If 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. @@ -736,6 +780,10 @@ public ILGenerator GetILGenerator() /// 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 , the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to , local variables are not initialized and the generated code is unverifiable. /// From 488b3c0c2076839382b862ea09b7d01dffa69e88 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Feb 2026 01:09:52 +0000 Subject: [PATCH 6/8] Convert examples to inline C# code and remove VB references Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../System/Reflection/Emit/DynamicMethod.cs | 130 ++++++++++++++---- 1 file changed, 106 insertions(+), 24 deletions(-) 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 4da5637a8f4822..fb4e014ef15a9f 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 @@ -451,8 +451,9 @@ private void Init(string name, /// /// A string representation of the dynamic method, showing the return type, name, and parameter types. /// - /// - /// + /// + /// Console.WriteLine("\r\nToString: {0}", hello.ToString()); + /// /// /// /// The signature includes only types and the method name, if any. Parameter names are not included. @@ -477,8 +478,11 @@ public override string ToString() /// /// The name of the dynamic method. /// - /// - /// + /// + /// // Display the name specified when the dynamic method was created. + /// // Note that the name can be blank. + /// Console.WriteLine("\r\nName: {0}", hello.Name); + /// /// /// /// It is not necessary to name dynamic methods. @@ -490,8 +494,18 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// - /// + /// + /// // 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); + /// } + /// /// /// /// This property always returns for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type. @@ -503,8 +517,17 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// - /// + /// + /// // 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); + /// } + /// /// /// /// This property always returns for dynamic methods. @@ -516,8 +539,10 @@ public override string ToString() /// /// The associated with the dynamic method. /// - /// - /// + /// + /// // Display the module specified when the dynamic method was created. + /// Console.WriteLine("\r\nModule: {0}", hello.Module); + /// /// /// /// 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. @@ -537,8 +562,11 @@ public override string ToString() /// /// A bitwise combination of the values representing the attributes for the method. /// - /// - /// + /// + /// // Display MethodAttributes for the dynamic method, set when + /// // the dynamic method was created. + /// Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes); + /// /// /// /// Currently, the method attributes for a dynamic method are always and . @@ -550,8 +578,11 @@ public override string ToString() /// /// One of the values that indicates the calling convention of the method. /// - /// - /// + /// + /// // Display the calling convention of the dynamic method, set when the + /// // dynamic method was created. + /// Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention); + /// /// /// /// Currently, the calling convention for a dynamic method is always . @@ -572,8 +603,16 @@ public override string ToString() /// /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. /// - /// - /// + /// + /// // 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); + /// } + /// /// /// /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters. @@ -695,8 +734,10 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// A representing the return type of the dynamic method; or if the method has no return type. /// - /// - /// + /// + /// // If the method has no return type, ReturnType is System.Void. + /// Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType); + /// /// /// /// If was specified for the return type when the dynamic method was created, this property returns . @@ -717,8 +758,33 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// An representing the custom attributes of the return type for the dynamic method. /// - /// - /// + /// + /// // 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()); + /// } + /// } + /// } + /// /// /// /// 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. @@ -742,8 +808,14 @@ public override bool IsDefined(Type attributeType, bool inherit) /// -or- /// is greater than the number of parameters of the dynamic method. /// - /// - /// + /// + /// // 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"); + /// /// /// /// If is 0, the method refers to the return value. Setting parameter information has no effect on the return value. @@ -781,8 +853,18 @@ public ILGenerator GetILGenerator() /// /// if the local variables in the method are zero-initialized; otherwise, . The default is . /// - /// - /// + /// + /// // 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); + /// /// /// /// If this property is set to , the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to , local variables are not initialized and the generated code is unverifiable. From 78021002b8768e9e0c5691a889fb30f3ce65d3fa Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Feb 2026 01:59:55 +0000 Subject: [PATCH 7/8] Move code examples to separate file with #region blocks Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../Reflection/Emit/DynamicMethod.Examples.cs | 143 ++++++++++++++++++ .../System/Reflection/Emit/DynamicMethod.cs | 118 ++------------- 2 files changed, 155 insertions(+), 106 deletions(-) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs new file mode 100644 index 00000000000000..92cdd2ae37e4d3 --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs @@ -0,0 +1,143 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// This file contains code examples for XML documentation. +// The examples are referenced by the DynamicMethod class documentation using #region names. + +#pragma warning disable CS8321 // Local function is declared but never used + +using System; +using System.Reflection; +using System.Reflection.Emit; + +namespace System.Reflection.Emit.Examples +{ + internal static class DynamicMethodExamples + { + private static void Examples() + { + // Note: 'hello' represents a DynamicMethod instance used in the examples below + + #region Name + // Display the name specified when the dynamic method was created. + // Note that the name can be blank. + Console.WriteLine("\r\nName: {0}", hello.Name); + #endregion + + #region DeclaringType + // 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); + } + #endregion + + #region ReflectedType + // 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); + } + #endregion + + #region Module + // Display the module specified when the dynamic method was created. + Console.WriteLine("\r\nModule: {0}", hello.Module); + #endregion + + #region Attributes + // Display MethodAttributes for the dynamic method, set when + // the dynamic method was created. + Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes); + #endregion + + #region CallingConvention + // Display the calling convention of the dynamic method, set when the + // dynamic method was created. + Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention); + #endregion + + #region GetParameters + // 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); + } + #endregion + + #region ReturnType + // If the method has no return type, ReturnType is System.Void. + Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType); + #endregion + + #region ReturnTypeCustomAttributes + // 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()); + } + } + } + #endregion + + #region DefineParameter + // 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"); + #endregion + + #region ToString + Console.WriteLine("\r\nToString: {0}", hello.ToString()); + #endregion + + #region InitLocals + // 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); + #endregion + } + + // Placeholder for the DynamicMethod variable used in examples + private static DynamicMethod hello = null!; + } +} 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 fb4e014ef15a9f..39882eb3ffd812 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 @@ -451,9 +451,7 @@ private void Init(string name, /// /// A string representation of the dynamic method, showing the return type, name, and parameter types. /// - /// - /// Console.WriteLine("\r\nToString: {0}", hello.ToString()); - /// + /// /// /// /// The signature includes only types and the method name, if any. Parameter names are not included. @@ -478,11 +476,7 @@ public override string ToString() /// /// The name of the dynamic method. /// - /// - /// // Display the name specified when the dynamic method was created. - /// // Note that the name can be blank. - /// Console.WriteLine("\r\nName: {0}", hello.Name); - /// + /// /// /// /// It is not necessary to name dynamic methods. @@ -494,18 +488,7 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// - /// // 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); - /// } - /// + /// /// /// /// This property always returns for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type. @@ -517,17 +500,7 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// - /// // 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); - /// } - /// + /// /// /// /// This property always returns for dynamic methods. @@ -539,10 +512,7 @@ public override string ToString() /// /// The associated with the dynamic method. /// - /// - /// // Display the module specified when the dynamic method was created. - /// Console.WriteLine("\r\nModule: {0}", hello.Module); - /// + /// /// /// /// 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. @@ -562,11 +532,7 @@ public override string ToString() /// /// A bitwise combination of the values representing the attributes for the method. /// - /// - /// // Display MethodAttributes for the dynamic method, set when - /// // the dynamic method was created. - /// Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes); - /// + /// /// /// /// Currently, the method attributes for a dynamic method are always and . @@ -578,11 +544,7 @@ public override string ToString() /// /// One of the values that indicates the calling convention of the method. /// - /// - /// // Display the calling convention of the dynamic method, set when the - /// // dynamic method was created. - /// Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention); - /// + /// /// /// /// Currently, the calling convention for a dynamic method is always . @@ -603,16 +565,7 @@ public override string ToString() /// /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. /// - /// - /// // 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); - /// } - /// + /// /// /// /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters. @@ -734,10 +687,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// A representing the return type of the dynamic method; or if the method has no return type. /// - /// - /// // If the method has no return type, ReturnType is System.Void. - /// Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType); - /// + /// /// /// /// If was specified for the return type when the dynamic method was created, this property returns . @@ -758,33 +708,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// An representing the custom attributes of the return type for the dynamic method. /// - /// - /// // 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()); - /// } - /// } - /// } - /// + /// /// /// /// 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. @@ -808,14 +732,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// -or- /// is greater than the number of parameters of the dynamic method. /// - /// - /// // 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"); - /// + /// /// /// /// If is 0, the method refers to the return value. Setting parameter information has no effect on the return value. @@ -853,18 +770,7 @@ public ILGenerator GetILGenerator() /// /// if the local variables in the method are zero-initialized; otherwise, . The default is . /// - /// - /// // 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); - /// + /// /// /// /// If this property is set to , the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to , local variables are not initialized and the generated code is unverifiable. From 277b31e64e89bc835b6ae6bdaf55f40f94dfe558 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:00:32 +0000 Subject: [PATCH 8/8] Move DynamicMethod.Examples.cs to samples directory Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com> --- .../Reflection/Emit/DynamicMethod.Examples.cs | 0 .../System/Reflection/Emit/DynamicMethod.cs | 24 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) rename src/libraries/System.Private.CoreLib/{src => samples}/System/Reflection/Emit/DynamicMethod.Examples.cs (100%) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs b/src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Examples.cs similarity index 100% rename from src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.Examples.cs rename to src/libraries/System.Private.CoreLib/samples/System/Reflection/Emit/DynamicMethod.Examples.cs 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 39882eb3ffd812..b4be78b9451c46 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 @@ -451,7 +451,7 @@ private void Init(string name, /// /// A string representation of the dynamic method, showing the return type, name, and parameter types. /// - /// + /// /// /// /// The signature includes only types and the method name, if any. Parameter names are not included. @@ -476,7 +476,7 @@ public override string ToString() /// /// The name of the dynamic method. /// - /// + /// /// /// /// It is not necessary to name dynamic methods. @@ -488,7 +488,7 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// + /// /// /// /// This property always returns for dynamic methods. Even when a dynamic method is logically associated with a type, it is not declared by the type. @@ -500,7 +500,7 @@ public override string ToString() /// /// Always for dynamic methods. /// - /// + /// /// /// /// This property always returns for dynamic methods. @@ -512,7 +512,7 @@ public override string ToString() /// /// The associated with the dynamic method. /// - /// + /// /// /// /// 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. @@ -532,7 +532,7 @@ public override string ToString() /// /// A bitwise combination of the values representing the attributes for the method. /// - /// + /// /// /// /// Currently, the method attributes for a dynamic method are always and . @@ -544,7 +544,7 @@ public override string ToString() /// /// One of the values that indicates the calling convention of the method. /// - /// + /// /// /// /// Currently, the calling convention for a dynamic method is always . @@ -565,7 +565,7 @@ public override string ToString() /// /// An array of objects representing the parameters of the dynamic method, or an empty array if the method has no parameters. /// - /// + /// /// /// /// The objects returned by this method are for information only. Use the method to set or change the characteristics of the parameters. @@ -687,7 +687,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// A representing the return type of the dynamic method; or if the method has no return type. /// - /// + /// /// /// /// If was specified for the return type when the dynamic method was created, this property returns . @@ -708,7 +708,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// /// 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. @@ -732,7 +732,7 @@ public override bool IsDefined(Type attributeType, bool inherit) /// -or- /// is greater than the number of parameters of the dynamic method. /// - /// + /// /// /// /// If is 0, the method refers to the return value. Setting parameter information has no effect on the return value. @@ -770,7 +770,7 @@ public ILGenerator GetILGenerator() /// /// if the local variables in the method are zero-initialized; otherwise, . The default is . /// - /// + /// /// /// /// If this property is set to , the emitted Microsoft intermediate language (MSIL) includes initialization of local variables. If it is set to , local variables are not initialized and the generated code is unverifiable.