DynamicMethod Constructor (String, Type, Type, Boolean)
Initializes an anonymously hosted dynamic method, specifying the method name, return type, parameter types, and whether just-in-time (JIT) visibility checks should be skipped for types and members accessed by the Microsoft intermediate language (MSIL) of the dynamic method.
Assembly: mscorlib (in mscorlib.dll)
public DynamicMethod( string name, Type returnType, Type parameterTypes, bool restrictedSkipVisibility )
- Type: System.String
The name of the dynamic method. This can be a zero-length string, but it cannot be null.
- Type: System.Type
A Type object that specifies the return type of the dynamic method, or null if the method has no return type.
- Type: System.Type
An array of Type objects specifying the types of the parameters of the dynamic method, or null if the method has no parameters.
- Type: System.Boolean
true to skip JIT visibility checks on types and members accessed by the MSIL of the dynamic method, with this restriction: the trust level of the assemblies that contain those types and members must be equal to or less than the trust level of the call stack that emits the dynamic method; otherwise, false.
The dynamic method that is created by this constructor is associated with an anonymous assembly instead of an existing type or module. The anonymous assembly exists only to provide a sandbox environment for dynamic methods, that is, to isolate them from other code. This environment makes it safe for the dynamic method to be emitted and executed by partially trusted code.
Anonymously hosted dynamic methods do not have automatic access to any types or members that are private, protected, or internal (Friend in Visual Basic). This is different from dynamic methods that are associated with an existing type or module, which have access to hidden members in their associated scope.
Specify true for restrictedSkipVisibility if your dynamic method has to access types or members that are private, protected, or internal. This gives the dynamic method restricted access to these members. That is, the members can be accessed only if the following conditions are met:
The target members belong to an assembly that has a level of trust equal to or lower than the call stack that emits the dynamic method.
The call stack that emits the dynamic method is granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag. This is always true when the code is executed with full trust. For partially trusted code, it is true only if the host explicitly grants the permission.
If the permission has not been granted, a security exception is thrown when CreateDelegate is called or when the dynamic method is invoked, not when this constructor is called. No special permissions are required to emit the dynamic method.
For example, a dynamic method that is created with restrictedSkipVisibility set to true can access a private member of any assembly on the call stack if the call stack has been granted restricted member access. If the dynamic method is created with partially trusted code on the call stack, it cannot access a private member of a type in a .NET Framework assembly, because such assemblies are fully trusted.
If restrictedSkipVisibility is false, JIT visibility checks are enforced. The code in the dynamic method has access to public methods of public classes, and exceptions are thrown if it tries to access types or members that are private, protected, or internal.
When an anonymously hosted dynamic method is constructed, the call stack of the emitting assembly is included. When the method is invoked, the permissions of the emitting call stack are used instead of the permissions of the actual caller. Thus, the dynamic method cannot execute at a higher level of privilege than that of the assembly that emitted it, even if it is passed to and executed by an assembly that has a higher trust level.
This constructor was introduced in the .NET Framework 3.5 or later.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.