diff options
Diffstat (limited to 'src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs')
-rw-r--r-- | src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs | 328 |
1 files changed, 186 insertions, 142 deletions
diff --git a/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs b/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs index b7f76b90aa..9aae25c715 100644 --- a/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs +++ b/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs @@ -58,7 +58,7 @@ namespace System.Reflection.Emit // it is ready for use since there is not API which indictates that IL generation has completed. private static volatile InternalModuleBuilder s_anonymouslyHostedDynamicMethodsModule; private static readonly object s_anonymouslyHostedDynamicMethodsModuleLock = new object(); - + // // class initialization (ctor and init) // @@ -72,10 +72,10 @@ namespace System.Reflection.Emit { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - Init(name, - MethodAttributes.Public | MethodAttributes.Static, - CallingConventions.Standard, - returnType, + Init(name, + MethodAttributes.Public | MethodAttributes.Static, + CallingConventions.Standard, + returnType, parameterTypes, null, // owner null, // m @@ -105,10 +105,11 @@ namespace System.Reflection.Emit } [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Module m) { + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Module m) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(m, ref stackMark, false); Init(name, @@ -124,11 +125,12 @@ namespace System.Reflection.Emit } [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Module m, - bool skipVisibility) { + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Module m, + bool skipVisibility) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(m, ref stackMark, skipVisibility); Init(name, @@ -144,13 +146,14 @@ namespace System.Reflection.Emit } [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - MethodAttributes attributes, - CallingConventions callingConvention, - Type returnType, - Type[] parameterTypes, - Module m, - bool skipVisibility) { + public DynamicMethod(string name, + MethodAttributes attributes, + CallingConventions callingConvention, + Type returnType, + Type[] parameterTypes, + Module m, + bool skipVisibility) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(m, ref stackMark, skipVisibility); Init(name, @@ -166,16 +169,17 @@ namespace System.Reflection.Emit } [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Type owner) { + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Type owner) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(owner, ref stackMark, false); - Init(name, - MethodAttributes.Public | MethodAttributes.Static, - CallingConventions.Standard, - returnType, + Init(name, + MethodAttributes.Public | MethodAttributes.Static, + CallingConventions.Standard, + returnType, parameterTypes, owner, // owner null, // m @@ -183,52 +187,55 @@ namespace System.Reflection.Emit false, ref stackMark); // transparentMethod } - + [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Type owner, - bool skipVisibility) { + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Type owner, + bool skipVisibility) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(owner, ref stackMark, skipVisibility); - Init(name, - MethodAttributes.Public | MethodAttributes.Static, - CallingConventions.Standard, - returnType, - parameterTypes, + Init(name, + MethodAttributes.Public | MethodAttributes.Static, + CallingConventions.Standard, + returnType, + parameterTypes, owner, // owner null, // m skipVisibility, false, ref stackMark); // transparentMethod } - + [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod - public DynamicMethod(string name, - MethodAttributes attributes, - CallingConventions callingConvention, - Type returnType, - Type[] parameterTypes, - Type owner, - bool skipVisibility) { + public DynamicMethod(string name, + MethodAttributes attributes, + CallingConventions callingConvention, + Type returnType, + Type[] parameterTypes, + Type owner, + bool skipVisibility) + { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; PerformSecurityCheck(owner, ref stackMark, skipVisibility); - Init(name, - attributes, - callingConvention, - returnType, - parameterTypes, + Init(name, + attributes, + callingConvention, + returnType, + parameterTypes, owner, // owner null, // m - skipVisibility, + skipVisibility, false, ref stackMark); // transparentMethod } // helpers for intialization - static private void CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention) { + static private void CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention) + { // only static public for method attributes if ((attributes & ~MethodAttributes.MemberAccessMask) != MethodAttributes.Static) throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); @@ -239,7 +246,7 @@ namespace System.Reflection.Emit // only standard or varargs supported if (callingConvention != CallingConventions.Standard && callingConvention != CallingConventions.VarArgs) throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); - + // vararg is not supported at the moment if (callingConvention == CallingConventions.VarArgs) throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); @@ -283,13 +290,13 @@ namespace System.Reflection.Emit return s_anonymouslyHostedDynamicMethodsModule; } - private unsafe void Init(String name, - MethodAttributes attributes, - CallingConventions callingConvention, - Type returnType, - Type[] signature, - Type owner, - Module m, + private unsafe void Init(String name, + MethodAttributes attributes, + CallingConventions callingConvention, + Type returnType, + Type[] signature, + Type owner, + Module m, bool skipVisibility, bool transparentMethod, ref StackCrawlMark stackMark) @@ -297,23 +304,26 @@ namespace System.Reflection.Emit DynamicMethod.CheckConsistency(attributes, callingConvention); // check and store the signature - if (signature != null) { + if (signature != null) + { m_parameterTypes = new RuntimeType[signature.Length]; - for (int i = 0; i < signature.Length; i++) { - if (signature[i] == null) + for (int i = 0; i < signature.Length; i++) + { + if (signature[i] == null) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType; - if ( m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void) ) + if (m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void)) throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); } } - else { + else + { m_parameterTypes = Array.Empty<RuntimeType>(); } - + // check and store the return value m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : returnType.UnderlyingSystemType as RuntimeType; - if ( (m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef ) + if ((m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef) throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); if (transparentMethod) @@ -324,7 +334,6 @@ namespace System.Reflection.Emit { m_restrictedSkipVisibility = true; } - } else { @@ -359,7 +368,7 @@ namespace System.Reflection.Emit m_fInitLocals = true; m_methodHandle = null; - if (name == null) + if (name == null) throw new ArgumentNullException(nameof(name)); #if FEATURE_APPX @@ -378,7 +387,7 @@ namespace System.Reflection.Emit private void PerformSecurityCheck(Module m, ref StackCrawlMark stackMark, bool skipVisibility) { - if (m == null) + if (m == null) throw new ArgumentNullException(nameof(m)); Contract.EndContractBlock(); } @@ -393,7 +402,8 @@ namespace System.Reflection.Emit // Delegate and method creation // - public sealed override Delegate CreateDelegate(Type delegateType) { + public sealed override Delegate CreateDelegate(Type delegateType) + { if (m_restrictedSkipVisibility) { // Compile the method since accessibility checks are done as part of compilation. @@ -407,7 +417,8 @@ namespace System.Reflection.Emit return d; } - public sealed override Delegate CreateDelegate(Type delegateType, Object target) { + public sealed override Delegate CreateDelegate(Type delegateType, Object target) + { if (m_restrictedSkipVisibility) { // Compile the method since accessibility checks are done as part of compilation @@ -438,16 +449,21 @@ namespace System.Reflection.Emit #endif // This is guaranteed to return a valid handle - internal unsafe RuntimeMethodHandle GetMethodDescriptor() { - if (m_methodHandle == null) { - lock (this) { - if (m_methodHandle == null) { + internal unsafe RuntimeMethodHandle GetMethodDescriptor() + { + if (m_methodHandle == null) + { + lock (this) + { + if (m_methodHandle == null) + { if (m_DynamicILInfo != null) m_DynamicILInfo.GetCallableMethod(m_module, this); - else { + else + { if (m_ilGenerator == null || m_ilGenerator.ILOffset == 0) throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody", Name)); - + m_ilGenerator.GetCallableMethod(m_module, this); } } @@ -508,7 +524,8 @@ namespace System.Reflection.Emit get { return false; } } - public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) { + public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) + { if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs) throw new NotSupportedException(Environment.GetResourceString("NotSupported_CallToVarArg")); Contract.EndContractBlock(); @@ -554,7 +571,7 @@ namespace System.Reflection.Emit public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { - return m_dynMethod.GetCustomAttributes(attributeType, inherit); + return m_dynMethod.GetCustomAttributes(attributeType, inherit); } public override Object[] GetCustomAttributes(bool inherit) { return m_dynMethod.GetCustomAttributes(inherit); } @@ -571,12 +588,14 @@ namespace System.Reflection.Emit // DynamicMethod specific methods // - public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, String parameterName) { + public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, String parameterName) + { if (position < 0 || position > m_parameterTypes.Length) throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_ParamSequence")); position--; // it's 1 based. 0 is the return value - - if (position >= 0) { + + if (position >= 0) + { ParameterInfo[] parameters = m_dynMethod.LoadParameters(); parameters[position].SetName(parameterName); parameters[position].SetAttributes(attributes); @@ -584,11 +603,12 @@ namespace System.Reflection.Emit return null; } - public ILGenerator GetILGenerator() { + public ILGenerator GetILGenerator() + { return GetILGenerator(64); } - public ILGenerator GetILGenerator(int streamSize) + public ILGenerator GetILGenerator(int streamSize) { if (m_ilGenerator == null) { @@ -599,16 +619,18 @@ namespace System.Reflection.Emit return m_ilGenerator; } - public bool InitLocals { - get {return m_fInitLocals;} - set {m_fInitLocals = value;} + public bool InitLocals + { + get { return m_fInitLocals; } + set { m_fInitLocals = value; } } // // Internal API // - - internal MethodInfo GetMethodInfo() { + + internal MethodInfo GetMethodInfo() + { return m_dynMethod; } @@ -620,78 +642,91 @@ namespace System.Reflection.Emit // This way the DynamicMethod creator is the only one responsible for DynamicMethod access, // and can control exactly who gets access to it. // - internal class RTDynamicMethod : MethodInfo { - + internal class RTDynamicMethod : MethodInfo + { internal DynamicMethod m_owner; - ParameterInfo[] m_parameters; - String m_name; - MethodAttributes m_attributes; - CallingConventions m_callingConvention; + private ParameterInfo[] m_parameters; + private String m_name; + private MethodAttributes m_attributes; + private CallingConventions m_callingConvention; // // ctors // - private RTDynamicMethod() {} + private RTDynamicMethod() { } - internal RTDynamicMethod(DynamicMethod owner, String name, MethodAttributes attributes, CallingConventions callingConvention) { + internal RTDynamicMethod(DynamicMethod owner, String name, MethodAttributes attributes, CallingConventions callingConvention) + { m_owner = owner; m_name = name; m_attributes = attributes; m_callingConvention = callingConvention; } - + // // MethodInfo api // - public override String ToString() { + public override String ToString() + { return ReturnType.FormatTypeName() + " " + FormatNameAndSig(); } - public override String Name { + public override String Name + { get { return m_name; } } - public override Type DeclaringType { + public override Type DeclaringType + { get { return null; } } - public override Type ReflectedType { + public override Type ReflectedType + { get { return null; } } - public override Module Module { + public override Module Module + { get { return m_owner.m_module; } } - public override RuntimeMethodHandle MethodHandle { - get { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInDynamicMethod")); } + public override RuntimeMethodHandle MethodHandle + { + get { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInDynamicMethod")); } } - public override MethodAttributes Attributes { + public override MethodAttributes Attributes + { get { return m_attributes; } - } + } - public override CallingConventions CallingConvention { + public override CallingConventions CallingConvention + { get { return m_callingConvention; } } - - public override MethodInfo GetBaseDefinition() { + + public override MethodInfo GetBaseDefinition() + { return this; } - + [Pure] - public override ParameterInfo[] GetParameters() { + public override ParameterInfo[] GetParameters() + { ParameterInfo[] privateParameters = LoadParameters(); ParameterInfo[] parameters = new ParameterInfo[privateParameters.Length]; Array.Copy(privateParameters, 0, parameters, 0, privateParameters.Length); return parameters; } - - public override MethodImplAttributes GetMethodImplementationFlags() { + + public override MethodImplAttributes GetMethodImplementationFlags() + { return MethodImplAttributes.IL | MethodImplAttributes.NoInlining; } - public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) { + public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) + { // We want the creator of the DynamicMethod to control who has access to the // DynamicMethod (just like we do for delegates). However, a user can get to // the corresponding RTDynamicMethod using Exception.TargetSite, StackFrame.GetMethod, etc. @@ -701,28 +736,31 @@ namespace System.Reflection.Emit throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "this"); } - public override Object[] GetCustomAttributes(Type attributeType, bool inherit) { + public override Object[] GetCustomAttributes(Type attributeType, bool inherit) + { if (attributeType == null) throw new ArgumentNullException(nameof(attributeType)); Contract.EndContractBlock(); - if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute))) + if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute))) return new Object[] { new MethodImplAttribute(GetMethodImplementationFlags()) }; else return EmptyArray<Object>.Value; } - public override Object[] GetCustomAttributes(bool inherit) { + public override Object[] GetCustomAttributes(bool inherit) + { // support for MethodImplAttribute PCA return new Object[] { new MethodImplAttribute(GetMethodImplementationFlags()) }; } - - public override bool IsDefined(Type attributeType, bool inherit) { + + public override bool IsDefined(Type attributeType, bool inherit) + { if (attributeType == null) throw new ArgumentNullException(nameof(attributeType)); Contract.EndContractBlock(); - if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute))) + if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute))) return true; else return false; @@ -751,11 +789,13 @@ namespace System.Reflection.Emit } } - public override ParameterInfo ReturnParameter { - get { return null; } + public override ParameterInfo ReturnParameter + { + get { return null; } } - public override ICustomAttributeProvider ReturnTypeCustomAttributes { + public override ICustomAttributeProvider ReturnTypeCustomAttributes + { get { return GetEmptyCAHolder(); } } @@ -763,45 +803,49 @@ namespace System.Reflection.Emit // private implementation // - internal ParameterInfo[] LoadParameters() { - if (m_parameters == null) { + internal ParameterInfo[] LoadParameters() + { + if (m_parameters == null) + { Type[] parameterTypes = m_owner.m_parameterTypes; ParameterInfo[] parameters = new ParameterInfo[parameterTypes.Length]; - for (int i = 0; i < parameterTypes.Length; i++) + for (int i = 0; i < parameterTypes.Length; i++) parameters[i] = new RuntimeParameterInfo(this, null, parameterTypes[i], i); - if (m_parameters == null) + if (m_parameters == null) // should we interlockexchange? m_parameters = parameters; } return m_parameters; } - + // private implementation of CA for the return type - private ICustomAttributeProvider GetEmptyCAHolder() { + private ICustomAttributeProvider GetEmptyCAHolder() + { return new EmptyCAHolder(); } /////////////////////////////////////////////////// // EmptyCAHolder - private class EmptyCAHolder : ICustomAttributeProvider { - internal EmptyCAHolder() {} + private class EmptyCAHolder : ICustomAttributeProvider + { + internal EmptyCAHolder() { } - Object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit) { + Object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit) + { return EmptyArray<Object>.Value; } - Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) { + Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) + { return EmptyArray<Object>.Value; } - bool ICustomAttributeProvider.IsDefined (Type attributeType, bool inherit) { + bool ICustomAttributeProvider.IsDefined(Type attributeType, bool inherit) + { return false; } } - } - } - } |