19 internal EmptyCAHolder()
25 return EmptyArray<object>.Value;
30 return EmptyArray<object>.Value;
43 private string m_name;
49 public override string Name => m_name;
81 private RTDynamicMethod()
89 m_attributes = attributes;
90 m_callingConvention = callingConvention;
95 return ReturnType.FormatTypeName() +
" " + FormatNameAndSig();
123 if (attributeType ==
null)
134 return EmptyArray<object>.
Value;
145 public override bool IsDefined(
Type attributeType,
bool inherit)
147 if (attributeType ==
null)
160 if (m_parameters ==
null)
162 Type[] parameterTypes = m_owner.m_parameterTypes;
164 for (
int i = 0; i < parameterTypes.Length; i++)
166 array[i] =
new RuntimeParameterInfo(
this,
null, parameterTypes[i], i);
168 if (m_parameters ==
null)
170 m_parameters = array;
178 return new EmptyCAHolder();
182 private RuntimeType[] m_parameterTypes;
184 internal IRuntimeMethodInfo m_methodHandle;
186 private RuntimeType m_returnType;
188 private DynamicILGenerator m_ilGenerator;
192 private bool m_fInitLocals;
194 private RuntimeModule m_module;
196 internal bool m_skipVisibility;
198 internal RuntimeType m_typeOwner;
200 private RTDynamicMethod m_dynMethod;
202 internal DynamicResolver m_resolver;
204 private bool m_profileAPICheck;
206 private RuntimeAssembly m_creatorAssembly;
208 internal bool m_restrictedSkipVisibility;
212 private static volatile InternalModuleBuilder s_anonymouslyHostedDynamicMethodsModule;
214 private static readonly
object s_anonymouslyHostedDynamicMethodsModuleLock =
new object();
216 internal bool ProfileAPICheck
220 return m_profileAPICheck;
222 [FriendAccessAllowed]
225 m_profileAPICheck = value;
231 public override string Name => m_dynMethod.Name;
270 [SecuritySafeCritical]
273 if (m_methodHandle !=
null)
277 if (m_typeOwner !=
null)
279 RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
280 return runtimeAssembly.IsAllSecurityCritical();
282 RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
283 return runtimeAssembly2.IsAllSecurityCritical();
293 [SecuritySafeCritical]
296 if (m_methodHandle !=
null)
300 if (m_typeOwner !=
null)
302 RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
303 return runtimeAssembly.IsAllPublicAreaSecuritySafeCritical();
305 RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
306 return runtimeAssembly2.IsAllSecuritySafeCritical();
316 [SecuritySafeCritical]
319 if (m_methodHandle !=
null)
323 if (m_typeOwner !=
null)
325 RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
326 return !runtimeAssembly.IsAllSecurityCritical();
328 RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
329 return !runtimeAssembly2.IsAllSecurityCritical();
352 return m_fInitLocals;
356 m_fInitLocals = value;
374 [SecuritySafeCritical]
377 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
393 [SecuritySafeCritical]
396 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
413 [SecuritySafeCritical]
416 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
417 PerformSecurityCheck(m, ref stackMark, skipVisibility:
false);
436 [SecuritySafeCritical]
439 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
440 PerformSecurityCheck(m, ref stackMark, skipVisibility);
463 [SecuritySafeCritical]
466 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
467 PerformSecurityCheck(m, ref stackMark, skipVisibility);
468 Init(name, attributes, callingConvention, returnType, parameterTypes,
null, m, skipVisibility, transparentMethod:
false, ref stackMark);
484 [SecuritySafeCritical]
487 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
488 PerformSecurityCheck(owner, ref stackMark, skipVisibility:
false);
507 [SecuritySafeCritical]
510 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
511 PerformSecurityCheck(owner, ref stackMark, skipVisibility);
534 [SecuritySafeCritical]
537 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
538 PerformSecurityCheck(owner, ref stackMark, skipVisibility);
539 Init(name, attributes, callingConvention, returnType, parameterTypes, owner,
null, skipVisibility, transparentMethod:
false, ref stackMark);
558 throw new NotSupportedException(Environment.GetResourceString(
"NotSupported_DynamicMethodFlags"));
564 private static RuntimeModule GetDynamicMethodsModule()
566 if (s_anonymouslyHostedDynamicMethodsModule !=
null)
568 return s_anonymouslyHostedDynamicMethodsModule;
570 lock (s_anonymouslyHostedDynamicMethodsModuleLock)
572 if (s_anonymouslyHostedDynamicMethodsModule !=
null)
574 return s_anonymouslyHostedDynamicMethodsModule;
577 CustomAttributeBuilder item =
new CustomAttributeBuilder(constructor, EmptyArray<object>.Value);
584 CustomAttributeBuilder item2 =
new CustomAttributeBuilder(constructor2,
new object[1]
589 AssemblyName name =
new AssemblyName(
"Anonymously Hosted DynamicMethods Assembly");
590 StackCrawlMark stackMark = StackCrawlMark.LookForMe;
592 AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(assemblyBuilder.GetNativeHandle());
593 s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder)assemblyBuilder.ManifestModule;
595 return s_anonymouslyHostedDynamicMethodsModule;
599 private void Init(
string name,
MethodAttributes attributes,
CallingConventions callingConvention, Type returnType, Type[] signature, Type owner,
Module m,
bool skipVisibility,
bool transparentMethod, ref StackCrawlMark stackMark)
601 CheckConsistency(attributes, callingConvention);
602 if (signature !=
null)
604 m_parameterTypes =
new RuntimeType[signature.Length];
605 for (
int i = 0; i < signature.Length; i++)
607 if (signature[i] ==
null)
609 throw new ArgumentException(Environment.GetResourceString(
"Arg_InvalidTypeInSignature"));
611 m_parameterTypes[i] = (signature[i].UnderlyingSystemType as RuntimeType);
612 if (m_parameterTypes[i] ==
null || (
object)m_parameterTypes[i] ==
null || m_parameterTypes[i] == (RuntimeType)typeof(
void))
614 throw new ArgumentException(Environment.GetResourceString(
"Arg_InvalidTypeInSignature"));
620 m_parameterTypes =
new RuntimeType[0];
622 m_returnType = ((returnType ==
null) ? ((RuntimeType)typeof(
void)) : (returnType.UnderlyingSystemType as RuntimeType));
623 if (m_returnType ==
null || (
object)m_returnType ==
null || m_returnType.IsByRef)
625 throw new NotSupportedException(Environment.GetResourceString(
"Arg_InvalidTypeInRetType"));
627 if (transparentMethod)
629 m_module = GetDynamicMethodsModule();
632 m_restrictedSkipVisibility =
true;
640 m_module = m.ModuleHandle.GetRuntimeModule();
644 RuntimeType runtimeType =
null;
647 runtimeType = (owner.UnderlyingSystemType as RuntimeType);
649 if (runtimeType !=
null)
651 if (runtimeType.HasElementType || runtimeType.ContainsGenericParameters || runtimeType.IsGenericParameter || runtimeType.IsInterface)
653 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidTypeForDynamicMethod"));
655 m_typeOwner = runtimeType;
656 m_module = runtimeType.GetRuntimeModule();
659 m_skipVisibility = skipVisibility;
661 m_ilGenerator =
null;
662 m_fInitLocals =
true;
663 m_methodHandle =
null;
666 throw new ArgumentNullException(
"name");
670 if (m_creatorAssembly ==
null)
672 m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
674 if (m_creatorAssembly !=
null && !m_creatorAssembly.IsFrameworkAssembly())
676 m_profileAPICheck =
true;
679 m_dynMethod =
new RTDynamicMethod(
this, name, attributes, callingConvention);
683 private void PerformSecurityCheck(
Module m, ref StackCrawlMark stackMark,
bool skipVisibility)
687 throw new ArgumentNullException(
"m");
689 ModuleBuilder moduleBuilder = m as ModuleBuilder;
690 RuntimeModule left = (!(moduleBuilder !=
null)) ? (m as RuntimeModule) : moduleBuilder.InternalModule;
693 throw new ArgumentException(Environment.GetResourceString(
"Argument_MustBeRuntimeModule"),
"m");
695 if (left == s_anonymouslyHostedDynamicMethodsModule)
697 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidValue"),
"m");
703 RuntimeType callerType = RuntimeMethodHandle.GetCallerType(ref stackMark);
704 m_creatorAssembly = callerType.GetRuntimeAssembly();
705 if (m.Assembly != m_creatorAssembly)
707 CodeAccessSecurityEngine.ReflectionTargetDemandHelper(PermissionType.SecurityControlEvidence, m.Assembly.PermissionSet);
712 private void PerformSecurityCheck(Type owner, ref StackCrawlMark stackMark,
bool skipVisibility)
716 throw new ArgumentNullException(
"owner");
718 RuntimeType runtimeType = owner as RuntimeType;
719 if (runtimeType ==
null)
721 runtimeType = (owner.UnderlyingSystemType as RuntimeType);
723 if (runtimeType ==
null)
725 throw new ArgumentNullException(
"owner", Environment.GetResourceString(
"Argument_MustBeRuntimeType"));
727 RuntimeType callerType = RuntimeMethodHandle.GetCallerType(ref stackMark);
732 else if (callerType != runtimeType)
736 m_creatorAssembly = callerType.GetRuntimeAssembly();
737 if (runtimeType.Assembly != m_creatorAssembly)
739 CodeAccessSecurityEngine.ReflectionTargetDemandHelper(PermissionType.SecurityControlEvidence, owner.Assembly.PermissionSet);
749 [SecuritySafeCritical]
753 if (m_restrictedSkipVisibility)
755 GetMethodDescriptor();
759 multicastDelegate.StoreDynamicMethod(GetMethodInfo());
760 return multicastDelegate;
771 [SecuritySafeCritical]
775 if (m_restrictedSkipVisibility)
777 GetMethodDescriptor();
781 multicastDelegate.StoreDynamicMethod(GetMethodInfo());
782 return multicastDelegate;
788 if (m_methodHandle ==
null)
792 if (m_methodHandle ==
null)
794 if (m_DynamicILInfo !=
null)
796 m_DynamicILInfo.GetCallableMethod(m_module,
this);
800 if (m_ilGenerator ==
null || m_ilGenerator.ILOffset == 0)
804 m_ilGenerator.GetCallableMethod(m_module,
this);
809 return new RuntimeMethodHandle(m_methodHandle);
816 return m_dynMethod.ToString();
830 return m_dynMethod.GetParameters();
837 return m_dynMethod.GetMethodImplementationFlags();
852 [SecuritySafeCritical]
860 Signature signature =
new Signature(m_methodHandle, m_parameterTypes, m_returnType,
CallingConvention);
861 int num = signature.Arguments.Length;
862 int num2 = (parameters !=
null) ? parameters.Length : 0;
870 object[] array = CheckArguments(parameters, binder, invokeAttr, culture, signature);
872 for (
int i = 0; i < array.Length; i++)
874 parameters[i] = array[i];
894 return m_dynMethod.GetCustomAttributes(attributeType, inherit);
903 return m_dynMethod.GetCustomAttributes(inherit);
914 return m_dynMethod.IsDefined(attributeType, inherit);
927 if (position < 0 || position > m_parameterTypes.Length)
935 array[position].SetName(parameterName);
936 array[position].SetAttributes(attributes);
943 [SecuritySafeCritical]
947 if (m_DynamicILInfo !=
null)
949 return m_DynamicILInfo;
957 if (m_DynamicILInfo ==
null)
962 return m_DynamicILInfo;
975 [SecuritySafeCritical]
978 if (m_ilGenerator ==
null)
981 m_ilGenerator =
new DynamicILGenerator(
this, signature, streamSize);
983 return m_ilGenerator;
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
Creates a dynamic method that is global to a module, specifying the method name, attributes,...
Performs reflection on a module.
sealed override Delegate CreateDelegate(Type delegateType)
Completes the dynamic method and creates a delegate that can be used to execute it.
Discovers the attributes of a parameter and provides access to parameter metadata.
MethodAttributes
Specifies flags for method attributes. These flags are defined in the corhdr.h file.
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
Creates a dynamic method, specifying the method name, attributes, calling convention,...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner)
Creates a dynamic method, specifying the method name, return type, parameter types,...
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m)
Creates a dynamic method that is global to a module, specifying the method name, return type,...
object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
ReflectionPermissionFlag
Specifies the permitted use of the N:System.Reflection and N:System.Reflection.Emit namespaces.
Defines and represents a dynamic method that can be compiled, executed, and discarded....
MethodImplAttributes
Specifies flags for the attributes of a method implementation.
Discovers the attributes of a method and provides access to method metadata.
Specifies that an assembly cannot cause an elevation of privilege.
override RuntimeMethodHandle MethodHandle
Not supported for dynamic methods.
byte [] GetSignature()
Adds the end token to the signature and marks the signature as finished, so no further tokens can be ...
Represents a multicast delegate; that is, a delegate that can have more than one element in its invoc...
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static void KeepAlive(object obj)
References the specified object, which makes it ineligible for garbage collection from the start of t...
override Type ReflectedType
Gets the class that was used in reflection to obtain the method.
SecurityContextSource
Identifies the source for the security context.
CallingConvention
Specifies the calling convention required to call methods implemented in unmanaged code.
Provides methods for building signatures.
override ParameterInfo ReturnParameter
Gets the return parameter of the dynamic method.
AssemblyBuilderAccess
Defines the access modes for a dynamic assembly.
override MethodAttributes Attributes
Gets the attributes specified when the dynamic method was created.
override ParameterInfo [] GetParameters()
Returns the parameters of the dynamic method.
Creates or associates parameter information.
override Module Module
Gets the module with which the dynamic method is logically associated.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
Creates a dynamic method that is global to a module, specifying the method name, return type,...
bool IsDefined(Type attributeType, bool inherit)
Indicates whether one or more instance of attributeType is defined on this member.
Generates Microsoft intermediate language (MSIL) instructions.
CallingConventions
Defines the valid calling conventions for a method.
Indicates the set of security rules the common language runtime should enforce for an assembly.
Provides information about, and means to manipulate, the current environment and platform....
override bool IsDefined(Type attributeType, bool inherit)
Indicates whether the specified custom attribute type is defined.
T:System.RuntimeMethodHandle is a handle to the internal metadata representation of a method.
override string Name
Gets the name of the dynamic method.
Finds or creates a T:System.AppDomain.
DynamicMethod(string name, Type returnType, Type[] parameterTypes)
Initializes an anonymously hosted dynamic method, specifying the method name, return type,...
override MethodInfo GetBaseDefinition()
Returns the base implementation for the method.
ILGenerator GetILGenerator(int streamSize)
Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL str...
ILGenerator GetILGenerator()
Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream ...
Specifies the details of how a method is implemented. This class cannot be inherited.
override MethodImplAttributes GetMethodImplementationFlags()
Returns the implementation flags for the method.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Represents type declarations: class types, interface types, array types, value types,...
sealed override Delegate CreateDelegate(Type delegateType, object target)
Completes the dynamic method and creates a delegate that can be used to execute it,...
ParameterAttributes
Defines the attributes that can be associated with a parameter. These are defined in CorHdr....
override bool IsSecuritySafeCritical
Gets a value that indicates whether the current dynamic method is security-safe-critical at the curre...
MethodImplOptions Value
Gets the T:System.Runtime.CompilerServices.MethodImplOptions value describing the attributed method.
MethodImplOptions
Defines the details of how a method is implemented.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
Creates a dynamic method, specifying the method name, return type, parameter types,...
Selects a member from a list of candidates, and performs type conversion from actual argument type to...
Controls the system garbage collector, a service that automatically reclaims unused memory.
override Type ReturnType
Gets the type of return value for the dynamic method.
SecurityRuleSet
Identifies the set of security rules the common language runtime should enforce for an assembly.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static CompressedStack Capture()
Captures the compressed stack from the current thread.
ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string parameterName)
Defines a parameter of the dynamic method.
override Type DeclaringType
Gets the type that declares the method, which is always null for dynamic methods.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
override object [] GetCustomAttributes(bool inherit)
Returns all the custom attributes defined for the method.
override string ToString()
Returns the signature of the method, represented as a string.
override ICustomAttributeProvider ReturnTypeCustomAttributes
Gets the custom attributes of the return type for the dynamic method.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
The exception that is thrown when the number of parameters for an invocation does not match the numbe...
override object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns the custom attributes of the specified type that have been applied to the method.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
Initializes an anonymously hosted dynamic method, specifying the method name, return type,...
Provides support for alternative ways to generate the Microsoft intermediate language (MSIL) and meta...
Provides methods for setting and capturing the compressed stack on the current thread....
override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
Invokes the dynamic method using the specified parameters, under the constraints of the specified bin...
The exception that is thrown when a method call is invalid for the object's current state.
static SignatureHelper GetMethodSigHelper(Module mod, Type returnType, Type[] parameterTypes)
Returns a signature helper for a method with a standard calling convention, given the method's module...
Provides information about a specific culture (called a locale for unmanaged code development)....
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
bool InitLocals
Gets or sets a value indicating whether the local variables in the method are zero-initialized.
override bool IsSecurityTransparent
Gets a value that indicates whether the current dynamic method is transparent at the current trust le...
SecurityPermissionFlag
Specifies access flags for the security permission object.
DynamicILInfo GetDynamicILInfo()
Returns a T:System.Reflection.Emit.DynamicILInfo object that can be used to generate a method body fr...
Controls access to non-public types and members through the N:System.Reflection APIs....
virtual bool IsAssignableFrom(Type c)
Determines whether an instance of a specified type can be assigned to an instance of the current type...
Provides custom attributes for reflection objects that support them.
Provides a set of static methods and properties that provide support for compilers....
override bool IsSecurityCritical
Gets a value that indicates whether the current dynamic method is security-critical or security-safe-...