From 850af143e31b812db8064a8e3dcb2c50e1f351dd Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:46:05 -0800 Subject: [PATCH 01/10] Checkpoint from Copilot CLI for coding agent session --- ...ction-emit-dynamicmethod-createdelegate.md | 53 ++++++++++++ ...stem-reflection-emit-dynamicmethod-ctor.md | 81 +++++++++++++++++++ ...tion-emit-dynamicmethod-defineparameter.md | 20 +++++ ...ction-emit-dynamicmethod-getilgenerator.md | 37 +++++++++ ...em-reflection-emit-dynamicmethod-invoke.md | 31 +++++++ ...n-emit-dynamicmethod-issecuritycritical.md | 41 ++++++++++ ...it-dynamicmethod-issecuritysafecritical.md | 41 ++++++++++ ...mit-dynamicmethod-issecuritytransparent.md | 41 ++++++++++ ...stem-reflection-emit-dynamicmethod-name.md | 19 +++++ 9 files changed, 364 insertions(+) create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md create mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md new file mode 100644 index 0000000000000..73b97a9bb51db --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md @@ -0,0 +1,53 @@ +--- +title: System.Reflection.Emit.DynamicMethod.CreateDelegate methods +description: Learn about the System.Reflection.Emit.DynamicMethod.CreateDelegate methods. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.CreateDelegate methods + +[!INCLUDE [context](includes/context.md)] + +## + +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. + +To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. + +### Examples + +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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: + +## + +This method overload creates a delegate bound to a particular object. Such a delegate is said to be closed over its first argument. Although the method is static, it acts as if it were an instance method; the instance is `target`. + +This method overload requires `target` to be of the same type as the first parameter of the dynamic method, or to be assignable to that type (for example, a derived class). The signature of `delegateType` has all the parameters of the dynamic method except the first. For example, if the dynamic method has the parameters , , and , then `delegateType` has the parameters and ; `target` is of type . + +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. + +To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. + +### Examples + +The following code example creates a delegate that binds a to an instance of a type, so that the method acts on the same instance each time it's invoked. + +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. + +> [!NOTE] +> In general, changing the internal fields of classes isn't good object-oriented coding practice. + +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`. + +> [!NOTE] +> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. + +The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source.cs" id="Snippet1"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source.vb" id="Snippet1"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md new file mode 100644 index 0000000000000..52d4369258275 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md @@ -0,0 +1,81 @@ +--- +title: System.Reflection.Emit.DynamicMethod constructors +description: Learn about the System.Reflection.Emit.DynamicMethod constructors. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod constructors + +[!INCLUDE [context](includes/context.md)] + +## constructor + +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 don't 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 flag. This is always true when the code is executed with full trust. For partially trusted code, it's true only if the host explicitly grants the permission. + + > [!IMPORTANT] + > If the permission hasn't 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's 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 can't 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 can't execute at a higher level of privilege than that of the assembly that emitted it, even if it's passed to and executed by an assembly that has a higher trust level. + +This constructor specifies the method attributes and , and the calling convention . + +> [!NOTE] +> This constructor was introduced in .NET Framework 3.5 or later. + +## constructor + +This constructor specifies method attributes and , calling convention , and doesn't 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`. + +> [!NOTE] +> For backward compatibility, this constructor demands with the flag if the following conditions are both true: `m` is a module other than the calling module, and the demand for with the flag has failed. If the demand for succeeds, the operation is allowed. + +### Examples + +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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: + +## constructor + +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 and , calling convention , and doesn't skip just-in-time (JIT) visibility checks. + +> [!NOTE] +> For backward compatibility, this constructor demands with the flag if the following conditions are both true: `owner` is in a module other than the calling module, and the demand for with the flag has failed. If the demand for succeeds, the operation is allowed. + +### Examples + +The following code example creates a that's 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. + +> [!NOTE] +> In general, changing the internal fields of classes isn't good object-oriented coding practice. + +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`. + +> [!NOTE] +> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. + +The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source.cs" id="Snippet1"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source.vb" id="Snippet1"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md new file mode 100644 index 0000000000000..aa615217e02a8 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md @@ -0,0 +1,20 @@ +--- +title: System.Reflection.Emit.DynamicMethod.DefineParameter method +description: Learn about the System.Reflection.Emit.DynamicMethod.DefineParameter method. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.DefineParameter method + +[!INCLUDE [context](includes/context.md)] + +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. + +## Examples + +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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet33"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet33"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md new file mode 100644 index 0000000000000..cd3f38a6b613e --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md @@ -0,0 +1,37 @@ +--- +title: System.Reflection.Emit.DynamicMethod.GetILGenerator methods +description: Learn about the System.Reflection.Emit.DynamicMethod.GetILGenerator methods. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.GetILGenerator methods + +[!INCLUDE [context](includes/context.md)] + +## + +After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. + +> [!NOTE] +> There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for . + +### Examples + +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. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: + +## + +After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. + +> [!NOTE] +> There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for . + +### Examples + +The following code example demonstrates this method overload. This code example is part of a larger example provided for the class. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet2"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet2"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md new file mode 100644 index 0000000000000..9202c64d1c51a --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md @@ -0,0 +1,31 @@ +--- +title: System.Reflection.Emit.DynamicMethod.Invoke method +description: Learn about the System.Reflection.Emit.DynamicMethod.Invoke method. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.Invoke method + +[!INCLUDE [context](includes/context.md)] + +In addition to the listed exceptions, the calling code should be prepared to catch any exceptions thrown by the dynamic method. + +Executing a dynamic method with a delegate created by the method is more efficient than executing it with the method. + +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. + +All dynamic methods are static, so the `obj` parameter is always ignored. To treat a dynamic method as if it were an instance method, use the overload that takes an object instance. + +If the dynamic method has no parameters, the value of `parameters` should be `null`. Otherwise the number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters of the dynamic method. + +> [!NOTE] +> This method overload is called by the method overload inherited from the class, so the preceding remarks apply to both overloads. + +This method doesn't demand permissions directly, but invoking the dynamic method can result in security demands, depending on the method. For example, no demands are made for anonymously hosted dynamic methods that are created with the `restrictedSkipVisibility` parameter set to `false`. On the other hand, if you create a method with `restrictedSkipVisibility` set to `true` so it can access a hidden member of a target assembly, the method causes a demand for the permissions of the target assembly plus with the flag. + +## Examples + +The following code example invokes a dynamic method with exact binding, using the US-English culture. This code example is part of a larger example provided for the class. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet4"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet4"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md new file mode 100644 index 0000000000000..6bae05b6ff73d --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md @@ -0,0 +1,41 @@ +--- +title: System.Reflection.Emit.DynamicMethod.IsSecurityCritical property +description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurityCritical property. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.IsSecurityCritical property + +[!INCLUDE [context](includes/context.md)] + +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's 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 don't have security annotations, so they're 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's associated with a trusted assembly (that is, a strong-named assembly that's 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). + + > [!NOTE] + > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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). diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md new file mode 100644 index 0000000000000..31e9004707e43 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md @@ -0,0 +1,41 @@ +--- +title: System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property +description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property + +[!INCLUDE [context](includes/context.md)] + +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's 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 don't have security annotations, so they're 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's associated with a trusted assembly (that is, a strong-named assembly that's 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). + + > [!NOTE] + > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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). diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md new file mode 100644 index 0000000000000..cf20060f09d53 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md @@ -0,0 +1,41 @@ +--- +title: System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property +description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property + +[!INCLUDE [context](includes/context.md)] + +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's 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 don't have security annotations, so they're 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's associated with a trusted assembly (that is, a strong-named assembly that's 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). + + > [!NOTE] + > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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). diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md new file mode 100644 index 0000000000000..9846db7c93c33 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md @@ -0,0 +1,19 @@ +--- +title: System.Reflection.Emit.DynamicMethod.Name property +description: Learn about the System.Reflection.Emit.DynamicMethod.Name property. +ms.date: 02/10/2026 +ai-usage: ai-assisted +--- +# System.Reflection.Emit.DynamicMethod.Name property + +[!INCLUDE [context](includes/context.md)] + +> [!NOTE] +> It's not necessary to name dynamic methods. + +## Examples + +The following code example displays the name of a dynamic method. This code example is part of a larger example provided for the class. + +:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet27"::: +:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet27"::: From ece7a982f379e0805818d2cfe094cffa2f59416d Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:17:45 -0800 Subject: [PATCH 02/10] clean up xrefs --- ...reflection-emit-dynamicmethod-createdelegate.md | 14 +++++++------- .../system-reflection-emit-dynamicmethod-ctor.md | 8 ++++---- ...eflection-emit-dynamicmethod-defineparameter.md | 4 ++-- ...reflection-emit-dynamicmethod-getilgenerator.md | 6 +++--- .../system-reflection-emit-dynamicmethod-invoke.md | 8 ++++---- ...ection-emit-dynamicmethod-issecuritycritical.md | 2 +- ...on-emit-dynamicmethod-issecuritysafecritical.md | 2 +- ...ion-emit-dynamicmethod-issecuritytransparent.md | 2 +- .../system-reflection-emit-dynamicmethod.md | 4 ++-- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md index 73b97a9bb51db..a01e835372448 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md @@ -10,13 +10,13 @@ ai-usage: ai-assisted ## -Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. -To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. +To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. ### Examples -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. +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. :::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: @@ -27,9 +27,9 @@ This method overload creates a delegate bound to a particular object. Such a del This method overload requires `target` to be of the same type as the first parameter of the dynamic method, or to be assignable to that type (for example, a derived class). The signature of `delegateType` has all the parameters of the dynamic method except the first. For example, if the dynamic method has the parameters , , and , then `delegateType` has the parameters and ; `target` is of type . -Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. -To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. +To create a method body for a dynamic method when you have your own MSIL generator, call the method to obtain a object. If you don't have your own MSIL generator, call the method to obtain an object that can be used to generate the method body. ### Examples @@ -42,10 +42,10 @@ The example code then creates a that > [!NOTE] > In general, changing the internal fields of classes isn't good object-oriented coding practice. -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 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`. > [!NOTE] -> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. +> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md index 52d4369258275..9303dc7a8ec2a 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md @@ -21,7 +21,7 @@ Specify `true` for `restrictedSkipVisibility` if your dynamic method has to acce - The call stack that emits the dynamic method is granted with the flag. This is always true when the code is executed with full trust. For partially trusted code, it's true only if the host explicitly grants the permission. > [!IMPORTANT] - > If the permission hasn't 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. + > If the permission hasn't 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's 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 can't access a private member of a type in a .NET Framework assembly, because such assemblies are fully trusted. @@ -45,7 +45,7 @@ The dynamic method created with this constructor has access to public and `inter ### Examples -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. +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. :::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: @@ -70,10 +70,10 @@ The example code then creates a that > [!NOTE] > In general, changing the internal fields of classes isn't good object-oriented coding practice. -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 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`. > [!NOTE] -> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. +> This is an example of the relaxed rules for delegate binding introduced in .NET Framework 2.0, along with new overloads of the method. For more information, see the class. The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md index aa615217e02a8..292b655f43891 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md @@ -8,9 +8,9 @@ ai-usage: ai-assisted [!INCLUDE [context](includes/context.md)] -If `position` is 0, the method refers to the return value. Setting parameter information has no effect on the return value. +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. +If the dynamic method has already been completed, by calling the or method, the method has no effect. No exception is thrown. ## Examples diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md index cd3f38a6b613e..15bcb99123463 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md @@ -10,21 +10,21 @@ ai-usage: ai-assisted ## -After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. +After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. > [!NOTE] > There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for . ### Examples -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. +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. :::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: :::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: ## -After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. +After a dynamic method has been completed, by calling the or method, any further attempt to add MSIL is ignored. No exception is thrown. > [!NOTE] > There are restrictions on unverifiable code in dynamic methods, even in some full-trust scenarios. See the "Verification" section in Remarks for . diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md index 9202c64d1c51a..894d0eee03aa0 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md @@ -10,16 +10,16 @@ ai-usage: ai-assisted In addition to the listed exceptions, the calling code should be prepared to catch any exceptions thrown by the dynamic method. -Executing a dynamic method with a delegate created by the method is more efficient than executing it with the method. +Executing a dynamic method with a delegate created by the method is more efficient than executing it with the method. -Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. +Calling the method or the method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown. -All dynamic methods are static, so the `obj` parameter is always ignored. To treat a dynamic method as if it were an instance method, use the overload that takes an object instance. +All dynamic methods are static, so the `obj` parameter is always ignored. To treat a dynamic method as if it were an instance method, use the overload that takes an object instance. If the dynamic method has no parameters, the value of `parameters` should be `null`. Otherwise the number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters of the dynamic method. > [!NOTE] -> This method overload is called by the method overload inherited from the class, so the preceding remarks apply to both overloads. +> This method overload is called by the method overload inherited from the class, so the preceding remarks apply to both overloads. This method doesn't demand permissions directly, but invoking the dynamic method can result in security demands, depending on the method. For example, no demands are made for anonymously hosted dynamic methods that are created with the `restrictedSkipVisibility` parameter set to `false`. On the other hand, if you create a method with `restrictedSkipVisibility` set to `true` so it can access a hidden member of a target assembly, the method causes a demand for the permissions of the target assembly plus with the flag. diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md index 6bae05b6ff73d..8d1836c3e6482 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md @@ -8,7 +8,7 @@ ai-usage: ai-assisted [!INCLUDE [context](includes/context.md)] -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: +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 | |----------------|--------------------|-----------------------|-----------------------| diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md index 31e9004707e43..30cdfc01b74f8 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md @@ -8,7 +8,7 @@ ai-usage: ai-assisted [!INCLUDE [context](includes/context.md)] -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: +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 | |----------------|--------------------|-----------------------|-----------------------| diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md index cf20060f09d53..9433737112df5 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md @@ -8,7 +8,7 @@ ai-usage: ai-assisted [!INCLUDE [context](includes/context.md)] -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: +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 | |----------------|--------------------|-----------------------|-----------------------| diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod.md index a8a052f0a7c06..c0e468fb99988 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod.md @@ -38,7 +38,7 @@ A dynamic method that is associated with a module has the permissions of that mo Dynamic methods and their parameters do not have to be named, but you can specify names to assist in debugging. Custom attributes are not supported on dynamic methods or their parameters. -Although dynamic methods are `static` methods (`Shared` methods in Visual Basic), the relaxed rules for delegate binding allow a dynamic method to be bound to an object, so that it acts like an instance method when called using that delegate instance. An example that demonstrates this is provided for the method overload. +Although dynamic methods are `static` methods (`Shared` methods in Visual Basic), the relaxed rules for delegate binding allow a dynamic method to be bound to an object, so that it acts like an instance method when called using that delegate instance. An example that demonstrates this is provided for the method overload. ## Verification @@ -49,4 +49,4 @@ The following list summarizes the conditions under which dynamic methods can con - If a dynamic method that contains unverifiable code is associated with an assembly that has level 2 transparency (such as mscorlib.dll), it throws an exception (injected by the JIT compiler) instead of making a security demand. See [Security-Transparent Code, Level 2](/dotnet/framework/misc/security-transparent-code-level-2). - An anonymously hosted dynamic method that contains unverifiable code always throws an exception. It can never skip verification, even if it is created and executed by fully trusted code. -The exception that's thrown for unverifiable code varies depending on the way the dynamic method is invoked. If you invoke a dynamic method by using a delegate returned from the method, a is thrown. If you invoke the dynamic method by using the method, a is thrown with an inner . +The exception that's thrown for unverifiable code varies depending on the way the dynamic method is invoked. If you invoke a dynamic method by using a delegate returned from the method, a is thrown. If you invoke the dynamic method by using the method, a is thrown with an inner . From a00b6728afa16c3334aab892eaf98f668eb98c99 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:39:31 -0800 Subject: [PATCH 03/10] add snippets and add to toc --- .../runtime-libraries/includes/context.md | 3 +- .../DynamicMethod/.ctor/csharp/Project.csproj | 8 + .../DynamicMethod/.ctor/csharp/source.cs | 145 ++++++++++ .../DynamicMethod/.ctor/csharp/source1.cs | 81 ++++++ .../DynamicMethod/.ctor/vb/source.vb | 157 +++++++++++ .../DynamicMethod/.ctor/vb/source1.vb | 92 +++++++ .../Overview/csharp/Project.csproj | 8 + .../DynamicMethod/Overview/csharp/source.cs | 256 ++++++++++++++++++ .../DynamicMethod/Overview/vb/source.vb | 246 +++++++++++++++++ ...ction-emit-dynamicmethod-createdelegate.md | 8 +- ...stem-reflection-emit-dynamicmethod-ctor.md | 8 +- ...tion-emit-dynamicmethod-defineparameter.md | 4 +- ...ction-emit-dynamicmethod-getilgenerator.md | 8 +- ...em-reflection-emit-dynamicmethod-invoke.md | 4 +- ...stem-reflection-emit-dynamicmethod-name.md | 4 +- docs/fundamentals/toc.yml | 22 ++ 16 files changed, 1035 insertions(+), 19 deletions(-) create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb diff --git a/docs/fundamentals/runtime-libraries/includes/context.md b/docs/fundamentals/runtime-libraries/includes/context.md index 89814007b303e..48e7573b5ca2e 100644 --- a/docs/fundamentals/runtime-libraries/includes/context.md +++ b/docs/fundamentals/runtime-libraries/includes/context.md @@ -1 +1,2 @@ -This article provides supplementary remarks to the reference documentation for this API. +> [!NOTE] +> This article provides supplementary remarks to the reference documentation for this API. diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj new file mode 100644 index 0000000000000..c02dc5044e769 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj @@ -0,0 +1,8 @@ + + + + Library + net6.0 + + + \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs new file mode 100644 index 0000000000000..80fb4ba4ff306 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs @@ -0,0 +1,145 @@ +// +using System; +using System.Reflection; +using System.Reflection.Emit; + +// These classes are for demonstration purposes. +// +public class Example +{ + private int id = 0; + public Example(int id) + { + this.id = id; + } + public int ID { get { return id; }} +} + +public class DerivedFromExample : Example +{ + public DerivedFromExample(int id) : base(id) {} +} + +// Two delegates are declared: UseLikeInstance treats the dynamic +// method as if it were an instance method, and UseLikeStatic +// treats the dynamic method in the ordinary fashion. +// +public delegate int UseLikeInstance(int newID); +public delegate int UseLikeStatic(Example ex, int newID); + +public class Demo +{ + public static void Main() + { + // This dynamic method changes the private id field. It has + // no name; it returns the old id value (return type int); + // it takes two parameters, an instance of Example and + // an int that is the new value of id; and it is declared + // with Example as the owner type, so it can access all + // members, public and private. + // + DynamicMethod changeID = new DynamicMethod( + "", + typeof(int), + new Type[] { typeof(Example), typeof(int) }, + typeof(Example) + ); + + // Get a FieldInfo for the private field 'id'. + FieldInfo fid = typeof(Example).GetField( + "id", + BindingFlags.NonPublic | BindingFlags.Instance + ); + + ILGenerator ilg = changeID.GetILGenerator(); + + // Push the current value of the id field onto the + // evaluation stack. It's an instance field, so load the + // instance of Example before accessing the field. + ilg.Emit(OpCodes.Ldarg_0); + ilg.Emit(OpCodes.Ldfld, fid); + + // Load the instance of Example again, load the new value + // of id, and store the new field value. + ilg.Emit(OpCodes.Ldarg_0); + ilg.Emit(OpCodes.Ldarg_1); + ilg.Emit(OpCodes.Stfld, fid); + + // The original value of the id field is now the only + // thing on the stack, so return from the call. + ilg.Emit(OpCodes.Ret); + + // Create a delegate that uses changeID in the ordinary + // way, as a static method that takes an instance of + // Example and an int. + // + UseLikeStatic uls = + (UseLikeStatic) changeID.CreateDelegate( + typeof(UseLikeStatic) + ); + + // Create an instance of Example with an id of 42. + // + Example ex = new Example(42); + + // Create a delegate that is bound to the instance of + // of Example. This is possible because the first + // parameter of changeID is of type Example. The + // delegate has all the parameters of changeID except + // the first. + UseLikeInstance uli = + (UseLikeInstance) changeID.CreateDelegate( + typeof(UseLikeInstance), + ex + ); + + // First, change the value of id by calling changeID as + // a static method, passing in the instance of Example. + // + Console.WriteLine( + "Change the value of id; previous value: {0}", + uls(ex, 1492) + ); + + // Change the value of id again using the delegate bound + // to the instance of Example. + // + Console.WriteLine( + "Change the value of id; previous value: {0}", + uli(2700) + ); + + Console.WriteLine("Final value of id: {0}", ex.ID); + + // Now repeat the process with a class that derives + // from Example. + // + DerivedFromExample dfex = new DerivedFromExample(71); + + uli = (UseLikeInstance) changeID.CreateDelegate( + typeof(UseLikeInstance), + dfex + ); + + Console.WriteLine( + "Change the value of id; previous value: {0}", + uls(dfex, 73) + ); + Console.WriteLine( + "Change the value of id; previous value: {0}", + uli(79) + ); + Console.WriteLine("Final value of id: {0}", dfex.ID); + } +} + +/* This code example produces the following output: + +Change the value of id; previous value: 42 +Change the value of id; previous value: 1492 +Final value of id: 2700 +Change the value of id; previous value: 71 +Change the value of id; previous value: 73 +Final value of id: 79 + */ +// \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs new file mode 100644 index 0000000000000..0f3aedd8df60a --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs @@ -0,0 +1,81 @@ +// +using System; +using System.Reflection; +using System.Reflection.Emit; +using Microsoft.VisualBasic; + +public class Test +{ + // Declare a delegate that will be used to execute the completed + // dynamic method. + private delegate int HelloInvoker(string msg, int ret); + + public static void Main() + { + // Create an array that specifies the types of the parameters + // of the dynamic method. This method has a string parameter + // and an int parameter. + Type[] helloArgs = {typeof(string), typeof(int)}; + + // Create a dynamic method with the name "Hello", a return type + // of int, and two parameters whose types are specified by the + // array helloArgs. Create the method in the module that + // defines the Test class. + DynamicMethod hello = new DynamicMethod("Hello", + typeof(int), + helloArgs, + typeof(Test).Module); + + // + // Create an array that specifies the parameter types of the + // overload of Console.WriteLine to be used in Hello. + Type[] writeStringArgs = {typeof(string)}; + // Get the overload of Console.WriteLine that has one + // String parameter. + MethodInfo writeString = + typeof(Console).GetMethod("WriteLine", writeStringArgs); + + // Get an ILGenerator and emit a body for the dynamic method. + ILGenerator il = hello.GetILGenerator(); + // Load the first argument, which is a string, onto the stack. + il.Emit(OpCodes.Ldarg_0); + // Call the overload of Console.WriteLine that prints a string. + il.EmitCall(OpCodes.Call, writeString, null); + // The Hello method returns the value of the second argument; + // to do this, load the onto the stack and return. + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Ret); + // + + // + // Create a delegate that represents the dynamic method. This + // action completes the method, and any further attempts to + // change the method will cause an exception. + HelloInvoker hi = + (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker)); + // + + // Use the delegate to execute the dynamic method. Save and + // print the return value. + int retval = hi("\r\nHello, World!", 42); + Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}", + retval); + + // Do it again, with different arguments. + retval = hi("\r\nHi, Mom!", 5280); + Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}", + retval); + + // + // Create an array of arguments to use with the Invoke method. + object[] invokeArgs = {"\r\nHello, World!", 42}; + // Invoke the dynamic method using the arguments. This is much + // slower than using the delegate, because you must create an + // array to contain the arguments, and ValueType arguments + // must be boxed. + object objRet = hello.Invoke(null, invokeArgs); + Console.WriteLine("hello.Invoke returned {0}", objRet); + // + } +} +// \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb new file mode 100644 index 0000000000000..4daa8540d9153 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb @@ -0,0 +1,157 @@ +' +Imports System.Reflection +Imports System.Reflection.Emit + +' These classes are for demonstration purposes. +' +Public Class Example + Private _id As Integer = 0 + + Public Sub New(ByVal newId As Integer) + _id = newId + End Sub + + Public ReadOnly Property ID() As Integer + Get + Return _id + End Get + End Property +End Class + +Public Class DerivedFromExample + Inherits Example + + Public Sub New(ByVal newId As Integer) + MyBase.New(newId) + End Sub +End Class + +' Two delegates are declared: UseLikeInstance treats the dynamic +' method as if it were an instance method, and UseLikeStatic +' treats the dynamic method in the ordinary fashion. +' +Public Delegate Function UseLikeInstance(ByVal newID As Integer) _ + As Integer +Public Delegate Function UseLikeStatic(ByVal ex As Example, _ + ByVal newID As Integer) As Integer + +Public Class Demo + + Public Shared Sub Main() + ' This dynamic method changes the private _id field. It + ' has no name; it returns the old _id value (return type + ' Integer); it takes two parameters, an instance of Example + ' and an Integer that is the new value of _id; and it is + ' declared with Example as the owner type, so it can + ' access all members, public and private. + ' + Dim changeID As New DynamicMethod( _ + "", _ + GetType(Integer), _ + New Type() {GetType(Example), GetType(Integer)}, _ + GetType(Example) _ + ) + + ' Get a FieldInfo for the private field '_id'. + Dim fid As FieldInfo = GetType(Example).GetField( _ + "_id", _ + BindingFlags.NonPublic Or BindingFlags.Instance _ + ) + + Dim ilg As ILGenerator = changeID.GetILGenerator() + + ' Push the current value of the id field onto the + ' evaluation stack. It's an instance field, so load the + ' instance of Example before accessing the field. + ilg.Emit(OpCodes.Ldarg_0) + ilg.Emit(OpCodes.Ldfld, fid) + + ' Load the instance of Example again, load the new value + ' of id, and store the new field value. + ilg.Emit(OpCodes.Ldarg_0) + ilg.Emit(OpCodes.Ldarg_1) + ilg.Emit(OpCodes.Stfld, fid) + + ' The original value of the id field is now the only + ' thing on the stack, so return from the call. + ilg.Emit(OpCodes.Ret) + + + ' Create a delegate that uses changeID in the ordinary + ' way, as a static method that takes an instance of + ' Example and an Integer. + ' + Dim uls As UseLikeStatic = CType( _ + changeID.CreateDelegate(GetType(UseLikeStatic)), _ + UseLikeStatic _ + ) + + ' Create an instance of Example with an id of 42. + ' + Dim ex As New Example(42) + + ' Create a delegate that is bound to the instance of + ' of Example. This is possible because the first + ' parameter of changeID is of type Example. The + ' delegate has all the parameters of changeID except + ' the first. + Dim uli As UseLikeInstance = CType( _ + changeID.CreateDelegate( _ + GetType(UseLikeInstance), _ + ex), _ + UseLikeInstance _ + ) + + ' First, change the value of _id by calling changeID as + ' a static method, passing in the instance of Example. + ' + Console.WriteLine( _ + "Change the value of _id; previous value: {0}", _ + uls(ex, 1492) _ + ) + + ' Change the value of _id again using the delegate + ' bound to the instance of Example. + ' + Console.WriteLine( _ + "Change the value of _id; previous value: {0}", _ + uli(2700) _ + ) + + Console.WriteLine("Final value of _id: {0}", ex.ID) + + + ' Now repeat the process with a class that derives + ' from Example. + ' + Dim dfex As New DerivedFromExample(71) + + uli = CType( _ + changeID.CreateDelegate( _ + GetType(UseLikeInstance), _ + dfex), _ + UseLikeInstance _ + ) + + Console.WriteLine( _ + "Change the value of _id; previous value: {0}", _ + uls(dfex, 73) _ + ) + Console.WriteLine( _ + "Change the value of _id; previous value: {0}", _ + uli(79) _ + ) + Console.WriteLine("Final value of _id: {0}", dfex.ID) + + End Sub +End Class + +' This code example produces the following output: +' +'Change the value of _id; previous value: 42 +'Change the value of _id; previous value: 1492 +'Final value of _id: 2700 +'Change the value of _id; previous value: 71 +'Change the value of _id; previous value: 73 +'Final value of _id: 79' +' \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb new file mode 100644 index 0000000000000..d59c166e9da92 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb @@ -0,0 +1,92 @@ +' +Imports System.Reflection +Imports System.Reflection.Emit + +Public Class Test + ' Declare a delegate that will be used to execute the completed + ' dynamic method. + Private Delegate Function HelloInvoker(ByVal msg As String, _ + ByVal ret As Integer) As Integer + + Public Shared Sub Main() + ' Create an array that specifies the types of the parameters + ' of the dynamic method. This method has a String parameter + ' and an Integer parameter. + Dim helloArgs() As Type = {GetType(String), GetType(Integer)} + + ' Create a dynamic method with the name "Hello", a return type + ' of Integer, and two parameters whose types are specified by + ' the array helloArgs. Create the method in the module that + ' defines the Test class. + Dim hello As New DynamicMethod("Hello", _ + GetType(Integer), _ + helloArgs, _ + GetType(Test).Module) + + ' + ' Create an array that specifies the parameter types of the + ' overload of Console.WriteLine to be used in Hello. + Dim writeStringArgs() As Type = {GetType(String)} + ' Get the overload of Console.WriteLine that has one + ' String parameter. + Dim writeString As MethodInfo = GetType(Console). _ + GetMethod("WriteLine", writeStringArgs) + + ' Get an ILGenerator and emit a body for the dynamic method. + Dim il As ILGenerator = hello.GetILGenerator() + ' Load the first argument, which is a string, onto the stack. + il.Emit(OpCodes.Ldarg_0) + ' Call the overload of Console.WriteLine that prints a string. + il.EmitCall(OpCodes.Call, writeString, Nothing) + ' The Hello method returns the value of the second argument; + ' to do this, load the onto the stack and return. + il.Emit(OpCodes.Ldarg_1) + il.Emit(OpCodes.Ret) + ' + + ' + ' Create a delegate that represents the dynamic method. This + ' action completes the method, and any further attempts to + ' change the method will cause an exception. + Dim hi As HelloInvoker = _ + hello.CreateDelegate(GetType(HelloInvoker)) + ' + + ' Use the delegate to execute the dynamic method. Save and + ' print the return value. + Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42) + Console.WriteLine("Executing delegate hi(""Hello, World!"", 42) returned " _ + & retval) + + ' Do it again, with different arguments. + retval = hi(vbCrLf & "Hi, Mom!", 5280) + Console.WriteLine("Executing delegate hi(""Hi, Mom!"", 5280) returned " _ + & retval) + + ' + ' Create an array of arguments to use with the Invoke method. + Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42} + ' Invoke the dynamic method using the arguments. This is much + ' slower than using the delegate, because you must create an + ' array to contain the arguments, and ValueType arguments + ' must be boxed. Note that this overload of Invoke is + ' inherited from MethodBase, and simply calls the more + ' complete overload of Invoke. + Dim objRet As Object = hello.Invoke(Nothing, invokeArgs) + Console.WriteLine("hello.Invoke returned " & objRet) + ' + End Sub +End Class + +' This code example produces the following output: +' +'Hello, World! +'Executing delegate hi("Hello, World!", 42) returned 42 +' +'Hi, Mom! +'Executing delegate hi("Hi, Mom!", 5280) returned 5280 +' +'Hello, World! +'hello.Invoke returned 42 +' +' diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj new file mode 100644 index 0000000000000..c02dc5044e769 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj @@ -0,0 +1,8 @@ + + + + Library + net6.0 + + + \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs new file mode 100644 index 0000000000000..7bc0477c0cfe6 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs @@ -0,0 +1,256 @@ +// +using System; +using System.Reflection; +using System.Reflection.Emit; +using System.Globalization; + +public class Test +{ + // Declare a delegate type that can be used to execute the completed + // dynamic method. + private delegate int HelloDelegate(string msg, int ret); + + public static void Main() + { + // Create an array that specifies the types of the parameters + // of the dynamic method. This dynamic method has a String + // parameter and an Integer parameter. + Type[] helloArgs = {typeof(string), typeof(int)}; + + // Create a dynamic method with the name "Hello", a return type + // of Integer, and two parameters whose types are specified by + // the array helloArgs. Create the method in the module that + // defines the String class. + DynamicMethod hello = new DynamicMethod("Hello", + typeof(int), + helloArgs, + typeof(string).Module); + + // + // Create an array that specifies the parameter types of the + // overload of Console.WriteLine to be used in Hello. + Type[] writeStringArgs = {typeof(string)}; + // Get the overload of Console.WriteLine that has one + // String parameter. + MethodInfo writeString = typeof(Console).GetMethod("WriteLine", + writeStringArgs); + + // Get an ILGenerator and emit a body for the dynamic method, + // using a stream size larger than the IL that will be + // emitted. + ILGenerator il = hello.GetILGenerator(256); + // Load the first argument, which is a string, onto the stack. + il.Emit(OpCodes.Ldarg_0); + // Call the overload of Console.WriteLine that prints a string. + il.EmitCall(OpCodes.Call, writeString, null); + // The Hello method returns the value of the second argument; + // to do this, load the onto the stack and return. + il.Emit(OpCodes.Ldarg_1); + il.Emit(OpCodes.Ret); + // + + // + // Add parameter information to the dynamic method. (This is not + // necessary, but can be useful for debugging.) For each parameter, + // identified by position, supply the parameter attributes and a + // parameter name. + hello.DefineParameter(1, ParameterAttributes.In, "message"); + hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn"); + // + + // + // Create a delegate that represents the dynamic method. This + // action completes the method. Any further attempts to + // change the method are ignored. + HelloDelegate hi = + (HelloDelegate) hello.CreateDelegate(typeof(HelloDelegate)); + + // Use the delegate to execute the dynamic method. + Console.WriteLine("\r\nUse the delegate to execute the dynamic method:"); + int retval = hi("\r\nHello, World!", 42); + Console.WriteLine("Invoking delegate hi(\"Hello, World!\", 42) returned: " + retval); + + // Execute it again, with different arguments. + retval = hi("\r\nHi, Mom!", 5280); + Console.WriteLine("Invoking delegate hi(\"Hi, Mom!\", 5280) returned: " + retval); + // + + // + Console.WriteLine("\r\nUse the Invoke method to execute the dynamic method:"); + // Create an array of arguments to use with the Invoke method. + object[] invokeArgs = {"\r\nHello, World!", 42}; + // Invoke the dynamic method using the arguments. This is much + // slower than using the delegate, because you must create an + // array to contain the arguments, and value-type arguments + // must be boxed. + object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us")); + Console.WriteLine("hello.Invoke returned: " + objRet); + // + + Console.WriteLine("\r\n ----- Display information about the dynamic method -----"); + // + // Display MethodAttributes for the dynamic method, set when + // the dynamic method was created. + Console.WriteLine("\r\nMethod Attributes: {0}", hello.Attributes); + // + + // + // Display the calling convention of the dynamic method, set when the + // dynamic method was created. + Console.WriteLine("\r\nCalling convention: {0}", hello.CallingConvention); + // + + // + // Display the declaring type, which is always null for dynamic + // methods. + if (hello.DeclaringType == null) + { + Console.WriteLine("\r\nDeclaringType is always null for dynamic methods."); + } + else + { + Console.WriteLine("DeclaringType: {0}", hello.DeclaringType); + } + // + + // + // Display the default value for InitLocals. + if (hello.InitLocals) + { + Console.Write("\r\nThis method contains verifiable code."); + } + else + { + Console.Write("\r\nThis method contains unverifiable code."); + } + Console.WriteLine(" (InitLocals = {0})", hello.InitLocals); + // + + // + // Display the module specified when the dynamic method was created. + Console.WriteLine("\r\nModule: {0}", hello.Module); + // + + // + // Display the name specified when the dynamic method was created. + // Note that the name can be blank. + Console.WriteLine("\r\nName: {0}", hello.Name); + // + + // + // For dynamic methods, the reflected type is always null. + if (hello.ReflectedType == null) + { + Console.WriteLine("\r\nReflectedType is null."); + } + else + { + Console.WriteLine("\r\nReflectedType: {0}", hello.ReflectedType); + } + // + + // + if (hello.ReturnParameter == null) + { + Console.WriteLine("\r\nMethod has no return parameter."); + } + else + { + Console.WriteLine("\r\nReturn parameter: {0}", hello.ReturnParameter); + } + // + + // + // If the method has no return type, ReturnType is System.Void. + Console.WriteLine("\r\nReturn type: {0}", hello.ReturnType); + // + + // + // ReturnTypeCustomAttributes returns an ICustomeAttributeProvider + // that can be used to enumerate the custom attributes of the + // return value. At present, there is no way to set such custom + // attributes, so the list is empty. + if (hello.ReturnType == typeof(void)) + { + Console.WriteLine("The method has no return type."); + } + else + { + ICustomAttributeProvider caProvider = hello.ReturnTypeCustomAttributes; + object[] returnAttributes = caProvider.GetCustomAttributes(true); + if (returnAttributes.Length == 0) + { + Console.WriteLine("\r\nThe return type has no custom attributes."); + } + else + { + Console.WriteLine("\r\nThe return type has the following custom attributes:"); + foreach( object attr in returnAttributes ) + { + Console.WriteLine("\t{0}", attr.ToString()); + } + } + } + // + + // + Console.WriteLine("\r\nToString: {0}", hello.ToString()); + // + + // + // Display parameter information. + ParameterInfo[] parameters = hello.GetParameters(); + Console.WriteLine("\r\nParameters: name, type, ParameterAttributes"); + foreach( ParameterInfo p in parameters ) + { + Console.WriteLine("\t{0}, {1}, {2}", + p.Name, p.ParameterType, p.Attributes); + } + // + } +} + +/* This code example produces the following output: + +Use the delegate to execute the dynamic method: + +Hello, World! +Invoking delegate hi("Hello, World!", 42) returned: 42 + +Hi, Mom! +Invoking delegate hi("Hi, Mom!", 5280) returned: 5280 + +Use the Invoke method to execute the dynamic method: + +Hello, World! +hello.Invoke returned: 42 + + ----- Display information about the dynamic method ----- + +Method Attributes: PrivateScope, Public, Static + +Calling convention: Standard + +DeclaringType is always null for dynamic methods. + +This method contains verifiable code. (InitLocals = True) + +Module: CommonLanguageRuntimeLibrary + +Name: Hello + +ReflectedType is null. + +Method has no return parameter. + +Return type: System.Int32 + +The return type has no custom attributes. + +ToString: Int32 Hello(System.String, Int32) + +Parameters: name, type, ParameterAttributes + message, System.String, In + valueToReturn, System.Int32, In + */ +// diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb new file mode 100644 index 0000000000000..fe82816c225a8 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb @@ -0,0 +1,246 @@ +' +Imports System.Reflection +Imports System.Reflection.Emit +Imports System.Globalization + +Public Class Test + ' Declare a delegate type that can be used to execute the completed + ' dynamic method. + Private Delegate Function HelloDelegate(ByVal msg As String, _ + ByVal ret As Integer) As Integer + + Public Shared Sub Main() + ' Create an array that specifies the types of the parameters + ' of the dynamic method. This dynamic method has a String + ' parameter and an Integer parameter. + Dim helloArgs() As Type = {GetType(String), GetType(Integer)} + + ' Create a dynamic method with the name "Hello", a return type + ' of Integer, and two parameters whose types are specified by + ' the array helloArgs. Create the method in the module that + ' defines the String class. + Dim hello As New DynamicMethod("Hello", _ + GetType(Integer), _ + helloArgs, _ + GetType(String).Module) + + ' + ' Create an array that specifies the parameter types of the + ' overload of Console.WriteLine to be used in Hello. + Dim writeStringArgs() As Type = {GetType(String)} + ' Get the overload of Console.WriteLine that has one + ' String parameter. + Dim writeString As MethodInfo = GetType(Console). _ + GetMethod("WriteLine", writeStringArgs) + + ' Get an ILGenerator and emit a body for the dynamic method, + ' using a stream size larger than the IL that will be + ' emitted. + Dim il As ILGenerator = hello.GetILGenerator(256) + ' Load the first argument, which is a string, onto the stack. + il.Emit(OpCodes.Ldarg_0) + ' Call the overload of Console.WriteLine that prints a string. + il.EmitCall(OpCodes.Call, writeString, Nothing) + ' The Hello method returns the value of the second argument; + ' to do this, load the onto the stack and return. + il.Emit(OpCodes.Ldarg_1) + il.Emit(OpCodes.Ret) + ' + + ' + ' Add parameter information to the dynamic method. (This is not + ' necessary, but can be useful for debugging.) For each parameter, + ' identified by position, supply the parameter attributes and a + ' parameter name. + hello.DefineParameter(1, ParameterAttributes.In, "message") + hello.DefineParameter(2, ParameterAttributes.In, "valueToReturn") + ' + + ' + ' Create a delegate that represents the dynamic method. This + ' action completes the method. Any further attempts to + ' change the method are ignored. + Dim hi As HelloDelegate = _ + CType(hello.CreateDelegate(GetType(HelloDelegate)), HelloDelegate) + + ' Use the delegate to execute the dynamic method. + Console.WriteLine(vbCrLf & "Use the delegate to execute the dynamic method:") + Dim retval As Integer = hi(vbCrLf & "Hello, World!", 42) + Console.WriteLine("Invoking delegate hi(""Hello, World!"", 42) returned: " _ + & retval & ".") + + ' Execute it again, with different arguments. + retval = hi(vbCrLf & "Hi, Mom!", 5280) + Console.WriteLine("Invoking delegate hi(""Hi, Mom!"", 5280) returned: " _ + & retval & ".") + ' + + ' + Console.WriteLine(vbCrLf & "Use the Invoke method to execute the dynamic method:") + ' Create an array of arguments to use with the Invoke method. + Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42} + ' Invoke the dynamic method using the arguments. This is much + ' slower than using the delegate, because you must create an + ' array to contain the arguments, and value-type arguments + ' must be boxed. + Dim objRet As Object = hello.Invoke(Nothing, _ + BindingFlags.ExactBinding, Nothing, invokeArgs, _ + New CultureInfo("en-us")) + Console.WriteLine("hello.Invoke returned: {0}", objRet) + ' + + Console.WriteLine(vbCrLf & _ + " ----- Display information about the dynamic method -----") + ' + ' Display MethodAttributes for the dynamic method, set when + ' the dynamic method was created. + Console.WriteLine(vbCrLf & "Method Attributes: {0}", _ + hello.Attributes) + ' + + ' + ' Display the calling convention of the dynamic method, set when the + ' dynamic method was created. + Console.WriteLine(vbCrLf & "Calling convention: {0}", _ + hello.CallingConvention) + ' + + ' + ' Display the declaring type, which is always Nothing for dynamic + ' methods. + If hello.DeclaringType Is Nothing Then + Console.WriteLine(vbCrLf & "DeclaringType is always Nothing for dynamic methods.") + Else + Console.WriteLine("DeclaringType: {0}", hello.DeclaringType) + End If + ' + + ' + ' Display the default value for InitLocals. + If hello.InitLocals Then + Console.Write(vbCrLf & "This method contains verifiable code.") + Else + Console.Write(vbCrLf & "This method contains unverifiable code.") + End If + Console.WriteLine(" (InitLocals = {0})", hello.InitLocals) + ' + + ' + ' Display the module specified when the dynamic method was created. + Console.WriteLine(vbCrLf & "Module: {0}", hello.Module) + ' + + ' + ' Display the name specified when the dynamic method was created. + ' Note that the name can be blank. + Console.WriteLine(vbCrLf & "Name: {0}", hello.Name) + ' + + ' + ' For dynamic methods, the reflected type is always Nothing. + If hello.ReflectedType Is Nothing Then + Console.WriteLine(vbCrLf & "ReflectedType is Nothing.") + Else + Console.WriteLine(vbCrLf & "ReflectedType: {0}", _ + hello.ReflectedType) + End If + ' + + ' + If hello.ReturnParameter Is Nothing Then + Console.WriteLine(vbCrLf & "Method has no return parameter.") + Else + Console.WriteLine(vbCrLf & "Return parameter: {0}", _ + hello.ReturnParameter) + End If + ' + + ' + ' If the method has no return type, ReturnType is System.Void. + Console.WriteLine(vbCrLf & "Return type: {0}", hello.ReturnType) + ' + + ' + ' ReturnTypeCustomAttributes returns an ICustomeAttributeProvider + ' that can be used to enumerate the custom attributes of the + ' return value. At present, there is no way to set such custom + ' attributes, so the list is empty. + If hello.ReturnType Is GetType(System.Void) Then + Console.WriteLine("The method has no return type.") + Else + Dim caProvider As ICustomAttributeProvider = _ + hello.ReturnTypeCustomAttributes + Dim returnAttributes() As Object = _ + caProvider.GetCustomAttributes(True) + If returnAttributes.Length = 0 Then + Console.WriteLine(vbCrLf _ + & "The return type has no custom attributes.") + Else + Console.WriteLine(vbCrLf _ + & "The return type has the following custom attributes:") + For Each attr As Object In returnAttributes + Console.WriteLine(vbTab & attr.ToString()) + Next attr + End If + End If + ' + + ' + Console.WriteLine(vbCrLf & "ToString: " & hello.ToString()) + ' + + ' + ' Display parameter information. + Dim parameters() As ParameterInfo = hello.GetParameters() + Console.WriteLine(vbCrLf & "Parameters: name, type, ParameterAttributes") + For Each p As ParameterInfo In parameters + Console.WriteLine(vbTab & "{0}, {1}, {2}", _ + p.Name, p.ParameterType, p.Attributes) + Next p + ' + End Sub +End Class + +' This code example produces the following output: +' +'Use the delegate to execute the dynamic method: +' +'Hello, World! +'Invoking delegate hi("Hello, World!", 42) returned: 42. +' +'Hi, Mom! +'Invoking delegate hi("Hi, Mom!", 5280) returned: 5280. +' +'Use the Invoke method to execute the dynamic method: +' +'Hello, World! +'hello.Invoke returned: 42 +' +' ----- Display information about the dynamic method ----- +' +'Method Attributes: PrivateScope, Public, Static +' +'Calling convention: Standard +' +'DeclaringType is always Nothing for dynamic methods. +' +'This method contains verifiable code. (InitLocals = True) +' +'Module: CommonLanguageRuntimeLibrary +' +'Name: Hello +' +'ReflectedType is Nothing. +' +'Method has no return parameter. +' +'Return type: System.Int32 +' +'The return type has no custom attributes. +' +'ToString: Int32 Hello(System.String, Int32) +' +'Parameters: name, type, ParameterAttributes +' message, System.String, In +' valueToReturn, System.Int32, In +' diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md index a01e835372448..70efbfe373755 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md @@ -18,8 +18,8 @@ To create a method body for a dynamic method when you have your own MSIL generat 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. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs" id="Snippet1"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb" id="Snippet1"::: ## @@ -49,5 +49,5 @@ The example code creates an instance of `Example` and then creates two delegates The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source.cs" id="Snippet1"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source.vb" id="Snippet1"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs" id="Snippet1"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb" id="Snippet1"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md index 9303dc7a8ec2a..10eb55a7301c1 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md @@ -47,8 +47,8 @@ The dynamic method created with this constructor has access to public and `inter 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. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs" id="Snippet1"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb" id="Snippet1"::: ## constructor @@ -77,5 +77,5 @@ The example code creates an instance of `Example` and then creates two delegates The `UseLikeStatic` delegate is invoked, passing in the instance of `Example` that's 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. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source.cs" id="Snippet1"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source.vb" id="Snippet1"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source.cs" id="Snippet1"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source.vb" id="Snippet1"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md index 292b655f43891..3405154357681 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md @@ -16,5 +16,5 @@ If the dynamic method has already been completed, by calling the class. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet33"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet33"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet33"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet33"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md index 15bcb99123463..3b09e1c0ad89f 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md @@ -19,8 +19,8 @@ After a dynamic method has been completed, by calling the method. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/.ctor/source1.cs" id="Snippet1"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/.ctor/source1.vb" id="Snippet1"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs" id="Snippet1"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb" id="Snippet1"::: ## @@ -33,5 +33,5 @@ After a dynamic method has been completed, by calling the class. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet2"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet2"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet2"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet2"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md index 894d0eee03aa0..79083875afcdb 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md @@ -27,5 +27,5 @@ This method doesn't demand permissions directly, but invoking the dynamic method The following code example invokes a dynamic method with exact binding, using the US-English culture. This code example is part of a larger example provided for the class. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet4"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet4"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet4"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet4"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md index 9846db7c93c33..322f6cc9420e3 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md @@ -15,5 +15,5 @@ ai-usage: ai-assisted The following code example displays the name of a dynamic method. This code example is part of a larger example provided for the class. -:::code language="csharp" source="~/snippets/csharp/System.Reflection.Emit/DynamicMethod/Overview/source.cs" id="Snippet27"::: -:::code language="vb" source="~/snippets/visualbasic/System.Reflection.Emit/DynamicMethod/Overview/source.vb" id="Snippet27"::: +:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet27"::: +:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet27"::: diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 972710a954a28..0e5837538d523 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -1537,6 +1537,28 @@ items: - name: The AssemblyBuilder class href: runtime-libraries/system-reflection-emit-assemblybuilder.md - name: The DynamicMethod class + items: + - name: Type remarks + href: runtime-libraries/system-reflection-emit-dynamicmethod.md + displayName: dynamicmethod + - name: CreateDelegate method + href: runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md + - name: Constructors + href: runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md + - name: DefineParameter method + href: runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md + - name: GetILGenerator methods + href: runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md + - name: Invoke method + href: runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md + - name: IsSecurityCritical property + href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md + - name: IsSecuritySafeCritical property + href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md + - name: IsSecurityTransparent property + href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md + - name: Name property + href: runtime-libraries/system-reflection-emit-dynamicmethod-name.md href: runtime-libraries/system-reflection-emit-dynamicmethod.md - name: The MethodBuilder class href: runtime-libraries/system-reflection-emit-methodbuilder.md From b46dc5ca2d6b66c89d76686b44f1434b1efcd1e2 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:44:37 -0800 Subject: [PATCH 04/10] target .net 10 --- .../DynamicMethod/Overview/csharp/Project.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj index c02dc5044e769..a369cfa8a8090 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/Project.csproj @@ -2,7 +2,7 @@ Library - net6.0 + net10.0 \ No newline at end of file From 0ad405dc58ab00cf9fb7a74d676e59c0c1373702 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:45:20 -0800 Subject: [PATCH 05/10] target .net 10 --- .../DynamicMethod/.ctor/csharp/Project.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj index c02dc5044e769..a369cfa8a8090 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/Project.csproj @@ -2,7 +2,7 @@ Library - net6.0 + net10.0 \ No newline at end of file From 0e1b9674758eeffccfd4680835b143cdec36f1e0 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 17:52:53 -0800 Subject: [PATCH 06/10] add missing project files --- .../DynamicMethod/.ctor/vb/Project.vbproj | 8 ++++++++ .../DynamicMethod/Overview/vb/Project.vbproj | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/Project.vbproj create mode 100644 docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/Project.vbproj diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/Project.vbproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/Project.vbproj new file mode 100644 index 0000000000000..a369cfa8a8090 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/Project.vbproj @@ -0,0 +1,8 @@ + + + + Library + net10.0 + + + \ No newline at end of file diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/Project.vbproj b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/Project.vbproj new file mode 100644 index 0000000000000..a369cfa8a8090 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/Project.vbproj @@ -0,0 +1,8 @@ + + + + Library + net10.0 + + + \ No newline at end of file From 0ae9ebf3a20f1bb6295e645903aed3d59b713a33 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:15:49 -0800 Subject: [PATCH 07/10] don't move very short remarks --- ...tion-emit-dynamicmethod-defineparameter.md | 20 ------------------- ...stem-reflection-emit-dynamicmethod-name.md | 19 ------------------ docs/fundamentals/toc.yml | 5 ----- 3 files changed, 44 deletions(-) delete mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md delete mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md deleted file mode 100644 index 3405154357681..0000000000000 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: System.Reflection.Emit.DynamicMethod.DefineParameter method -description: Learn about the System.Reflection.Emit.DynamicMethod.DefineParameter method. -ms.date: 02/10/2026 -ai-usage: ai-assisted ---- -# System.Reflection.Emit.DynamicMethod.DefineParameter method - -[!INCLUDE [context](includes/context.md)] - -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. - -## Examples - -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. - -:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet33"::: -:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet33"::: diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md deleted file mode 100644 index 322f6cc9420e3..0000000000000 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-name.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: System.Reflection.Emit.DynamicMethod.Name property -description: Learn about the System.Reflection.Emit.DynamicMethod.Name property. -ms.date: 02/10/2026 -ai-usage: ai-assisted ---- -# System.Reflection.Emit.DynamicMethod.Name property - -[!INCLUDE [context](includes/context.md)] - -> [!NOTE] -> It's not necessary to name dynamic methods. - -## Examples - -The following code example displays the name of a dynamic method. This code example is part of a larger example provided for the class. - -:::code language="csharp" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs" id="Snippet27"::: -:::code language="vb" source="./snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb" id="Snippet27"::: diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 0e5837538d523..2ca9476ece2cc 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -1545,8 +1545,6 @@ items: href: runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md - name: Constructors href: runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md - - name: DefineParameter method - href: runtime-libraries/system-reflection-emit-dynamicmethod-defineparameter.md - name: GetILGenerator methods href: runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md - name: Invoke method @@ -1557,9 +1555,6 @@ items: href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md - name: IsSecurityTransparent property href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md - - name: Name property - href: runtime-libraries/system-reflection-emit-dynamicmethod-name.md - href: runtime-libraries/system-reflection-emit-dynamicmethod.md - name: The MethodBuilder class href: runtime-libraries/system-reflection-emit-methodbuilder.md - name: The PersistedAssemblyBuilder class From c8d6337d52f1683ff076509b0f3f47c80a2fc4e8 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:32:09 -0800 Subject: [PATCH 08/10] add dev langs --- ...ction-emit-dynamicmethod-createdelegate.md | 3 ++ ...stem-reflection-emit-dynamicmethod-ctor.md | 3 ++ ...ction-emit-dynamicmethod-getilgenerator.md | 3 ++ ...em-reflection-emit-dynamicmethod-invoke.md | 3 ++ ...n-emit-dynamicmethod-issecuritycritical.md | 24 +++++------ ...it-dynamicmethod-issecuritysafecritical.md | 41 ------------------- ...mit-dynamicmethod-issecuritytransparent.md | 41 ------------------- docs/fundamentals/toc.yml | 6 +-- 8 files changed, 25 insertions(+), 99 deletions(-) delete mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md delete mode 100644 docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md index 70efbfe373755..7895e6c9d2f74 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-createdelegate.md @@ -3,6 +3,9 @@ title: System.Reflection.Emit.DynamicMethod.CreateDelegate methods description: Learn about the System.Reflection.Emit.DynamicMethod.CreateDelegate methods. ms.date: 02/10/2026 ai-usage: ai-assisted +dev_langs: + - CSharp + - VB --- # System.Reflection.Emit.DynamicMethod.CreateDelegate methods diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md index 10eb55a7301c1..6678f3c2156de 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-ctor.md @@ -3,6 +3,9 @@ title: System.Reflection.Emit.DynamicMethod constructors description: Learn about the System.Reflection.Emit.DynamicMethod constructors. ms.date: 02/10/2026 ai-usage: ai-assisted +dev_langs: + - CSharp + - VB --- # System.Reflection.Emit.DynamicMethod constructors diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md index 3b09e1c0ad89f..ab80ac9aaec08 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md @@ -3,6 +3,9 @@ title: System.Reflection.Emit.DynamicMethod.GetILGenerator methods description: Learn about the System.Reflection.Emit.DynamicMethod.GetILGenerator methods. ms.date: 02/10/2026 ai-usage: ai-assisted +dev_langs: + - CSharp + - VB --- # System.Reflection.Emit.DynamicMethod.GetILGenerator methods diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md index 79083875afcdb..799c9a113072b 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md @@ -3,6 +3,9 @@ title: System.Reflection.Emit.DynamicMethod.Invoke method description: Learn about the System.Reflection.Emit.DynamicMethod.Invoke method. ms.date: 02/10/2026 ai-usage: ai-assisted +dev_langs: + - CSharp + - VB --- # System.Reflection.Emit.DynamicMethod.Invoke method diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md index 8d1836c3e6482..f28100c74612e 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md @@ -1,10 +1,10 @@ --- -title: System.Reflection.Emit.DynamicMethod.IsSecurityCritical property -description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurityCritical property. +title: System.Reflection.Emit.DynamicMethod.IsSecurity* properties +description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurity*Critical* properties. ms.date: 02/10/2026 ai-usage: ai-assisted --- -# System.Reflection.Emit.DynamicMethod.IsSecurityCritical property +# System.Reflection.Emit.DynamicMethod.IsSecurity\* properties [!INCLUDE [context](includes/context.md)] @@ -24,17 +24,17 @@ The transparency of a dynamic method depends on the module it's associated with. - The transparency of a dynamic method that's associated with a trusted assembly (that is, a strong-named assembly that's 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 | + | 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). + For example, if you associate a dynamic method with a type that's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). - > [!NOTE] - > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't include the grant set of System.dll (that is, full trust), is thrown when the dynamic method is called. + > [!NOTE] + > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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. diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md deleted file mode 100644 index 30cdfc01b74f8..0000000000000 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property -description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property. -ms.date: 02/10/2026 -ai-usage: ai-assisted ---- -# System.Reflection.Emit.DynamicMethod.IsSecuritySafeCritical property - -[!INCLUDE [context](includes/context.md)] - -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's 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 don't have security annotations, so they're 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's associated with a trusted assembly (that is, a strong-named assembly that's 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). - - > [!NOTE] - > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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). diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md deleted file mode 100644 index 9433737112df5..0000000000000 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property -description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property. -ms.date: 02/10/2026 -ai-usage: ai-assisted ---- -# System.Reflection.Emit.DynamicMethod.IsSecurityTransparent property - -[!INCLUDE [context](includes/context.md)] - -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's 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 don't have security annotations, so they're 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's associated with a trusted assembly (that is, a strong-named assembly that's 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's in mscorlib.dll, which has level 2 mixed transparency, the dynamic method is transparent and can't 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). - - > [!NOTE] - > Associating a dynamic method with a module in a trusted level 1 assembly that's security-agnostic, such as System.dll, doesn't permit elevation of trust. If the grant set of the code that calls the dynamic method doesn't 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's 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). diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 2ca9476ece2cc..606e151dd3090 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -1549,12 +1549,8 @@ items: href: runtime-libraries/system-reflection-emit-dynamicmethod-getilgenerator.md - name: Invoke method href: runtime-libraries/system-reflection-emit-dynamicmethod-invoke.md - - name: IsSecurityCritical property + - name: IsSecurity* properties href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md - - name: IsSecuritySafeCritical property - href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritysafecritical.md - - name: IsSecurityTransparent property - href: runtime-libraries/system-reflection-emit-dynamicmethod-issecuritytransparent.md - name: The MethodBuilder class href: runtime-libraries/system-reflection-emit-methodbuilder.md - name: The PersistedAssemblyBuilder class From 636d55bf7cc5303c7e1dc16785fa048841e70ae8 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 10 Feb 2026 07:51:53 -0800 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../DynamicMethod/.ctor/csharp/source1.cs | 7 +++---- .../DynamicMethod/.ctor/vb/source1.vb | 4 ++-- .../DynamicMethod/Overview/csharp/source.cs | 2 +- .../DynamicMethod/Overview/vb/source.vb | 2 +- ...tem-reflection-emit-dynamicmethod-issecuritycritical.md | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs index 0f3aedd8df60a..c66da33c26c77 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/csharp/source1.cs @@ -2,7 +2,6 @@ using System; using System.Reflection; using System.Reflection.Emit; -using Microsoft.VisualBasic; public class Test { @@ -42,15 +41,15 @@ public static void Main() // Call the overload of Console.WriteLine that prints a string. il.EmitCall(OpCodes.Call, writeString, null); // The Hello method returns the value of the second argument; - // to do this, load the onto the stack and return. + // to do this, load the second argument onto the stack and return. il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ret); // // // Create a delegate that represents the dynamic method. This - // action completes the method, and any further attempts to - // change the method will cause an exception. + // action completes the method. Further attempts to change the + // method are ignored and no exception is thrown. HelloInvoker hi = (HelloInvoker) hello.CreateDelegate(typeof(HelloInvoker)); // diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb index d59c166e9da92..4f699871bbf82 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/.ctor/vb/source1.vb @@ -39,7 +39,7 @@ Public Class Test ' Call the overload of Console.WriteLine that prints a string. il.EmitCall(OpCodes.Call, writeString, Nothing) ' The Hello method returns the value of the second argument; - ' to do this, load the onto the stack and return. + ' to do this, load the second argument onto the stack and return. il.Emit(OpCodes.Ldarg_1) il.Emit(OpCodes.Ret) ' @@ -47,7 +47,7 @@ Public Class Test ' ' Create a delegate that represents the dynamic method. This ' action completes the method, and any further attempts to - ' change the method will cause an exception. + ' change the method are ignored and don't throw an exception. Dim hi As HelloInvoker = _ hello.CreateDelegate(GetType(HelloInvoker)) ' diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs index 7bc0477c0cfe6..b8249adec28c3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/csharp/source.cs @@ -44,7 +44,7 @@ public static void Main() // Call the overload of Console.WriteLine that prints a string. il.EmitCall(OpCodes.Call, writeString, null); // The Hello method returns the value of the second argument; - // to do this, load the onto the stack and return. + // to do this, load the second argument onto the stack and return. il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ret); // diff --git a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb index fe82816c225a8..ac1c66228dbe3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb +++ b/docs/fundamentals/runtime-libraries/snippets/System.Reflection.Emit/DynamicMethod/Overview/vb/source.vb @@ -42,7 +42,7 @@ Public Class Test ' Call the overload of Console.WriteLine that prints a string. il.EmitCall(OpCodes.Call, writeString, Nothing) ' The Hello method returns the value of the second argument; - ' to do this, load the onto the stack and return. + ' to do this, load the second argument onto the stack and return. il.Emit(OpCodes.Ldarg_1) il.Emit(OpCodes.Ret) ' diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md index f28100c74612e..ac3752bee3399 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md @@ -1,6 +1,6 @@ --- title: System.Reflection.Emit.DynamicMethod.IsSecurity* properties -description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurity*Critical* properties. +description: Learn about the System.Reflection.Emit.DynamicMethod.IsSecurity* properties. ms.date: 02/10/2026 ai-usage: ai-assisted --- From 99d2819396b9a22476d9331127847bb0bf76dfb5 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 10 Feb 2026 10:44:16 -0800 Subject: [PATCH 10/10] Update docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md --- .../system-reflection-emit-dynamicmethod-issecuritycritical.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md index ac3752bee3399..750eb0bf2b6e8 100644 --- a/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md +++ b/docs/fundamentals/runtime-libraries/system-reflection-emit-dynamicmethod-issecuritycritical.md @@ -8,6 +8,9 @@ ai-usage: ai-assisted [!INCLUDE [context](includes/context.md)] +> [!NOTE] +> The remarks in this article apply only to .NET Framework and not to modern .NET. + 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 |