diff options
Diffstat (limited to 'src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs')
-rw-r--r-- | src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs | 484 |
1 files changed, 233 insertions, 251 deletions
diff --git a/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs b/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs index f1d99d3c2c..2d2d3097a1 100644 --- a/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs +++ b/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs @@ -42,13 +42,6 @@ namespace System.Reflection.Emit // assigned by the DynamicResolver ctor internal DynamicResolver m_resolver; - // Always false unless we are in an immersive (non dev mode) process. -#if FEATURE_APPX - private bool m_profileAPICheck; - - private RuntimeAssembly m_creatorAssembly; -#endif - internal bool m_restrictedSkipVisibility; // The context when the method was created. We use this to do the RestrictedMemberAccess checks. // These checks are done when the method is compiled. This can happen at an arbitrary time, @@ -58,40 +51,33 @@ 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) // private DynamicMethod() { } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public DynamicMethod(string name, Type returnType, Type[] parameterTypes) { - 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 false, // skipVisibility - true, - ref stackMark); // transparentMethod + true); } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, @@ -100,17 +86,17 @@ namespace System.Reflection.Emit null, // owner null, // m restrictedSkipVisibility, - true, - ref stackMark); // transparentMethod + true); } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Module m) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - PerformSecurityCheck(m, ref stackMark, false); + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Module m) + { + if (m == null) + throw new ArgumentNullException(nameof(m)); + Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, @@ -119,18 +105,18 @@ namespace System.Reflection.Emit null, // owner m, // m false, // skipVisibility - false, - ref stackMark); // transparentMethod + false); } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - public DynamicMethod(string name, - Type returnType, - Type[] parameterTypes, - Module m, - bool skipVisibility) { - StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; - PerformSecurityCheck(m, ref stackMark, skipVisibility); + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Module m, + bool skipVisibility) + { + if (m == null) + throw new ArgumentNullException(nameof(m)); + Init(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, @@ -139,20 +125,20 @@ namespace System.Reflection.Emit null, // owner m, // m skipVisibility, - false, - ref stackMark); // transparentMethod + false); } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - 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); + public DynamicMethod(string name, + MethodAttributes attributes, + CallingConventions callingConvention, + Type returnType, + Type[] parameterTypes, + Module m, + bool skipVisibility) + { + if (m == null) + throw new ArgumentNullException(nameof(m)); + Init(name, attributes, callingConvention, @@ -161,93 +147,93 @@ namespace System.Reflection.Emit null, // owner m, // m skipVisibility, - false, - ref stackMark); // transparentMethod + false); } - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - 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, + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Type owner) + { + if (owner == null) + throw new ArgumentNullException(nameof(owner)); + + Init(name, + MethodAttributes.Public | MethodAttributes.Static, + CallingConventions.Standard, + returnType, parameterTypes, owner, // owner null, // m false, // skipVisibility - false, - ref stackMark); // transparentMethod + false); } - - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - 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, + + public DynamicMethod(string name, + Type returnType, + Type[] parameterTypes, + Type owner, + bool skipVisibility) + { + if (owner == null) + throw new ArgumentNullException(nameof(owner)); + + Init(name, + MethodAttributes.Public | MethodAttributes.Static, + CallingConventions.Standard, + returnType, + parameterTypes, owner, // owner null, // m skipVisibility, - false, - ref stackMark); // transparentMethod + false); } - - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable - 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, + + public DynamicMethod(string name, + MethodAttributes attributes, + CallingConventions callingConvention, + Type returnType, + Type[] parameterTypes, + Type owner, + bool skipVisibility) + { + if (owner == null) + throw new ArgumentNullException(nameof(owner)); + + Init(name, + attributes, + callingConvention, + returnType, + parameterTypes, owner, // owner null, // m - skipVisibility, - false, - ref stackMark); // transparentMethod + skipVisibility, + false); } // 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")); + throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags); if ((attributes & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); + throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags); Contract.EndContractBlock(); // only standard or varargs supported if (callingConvention != CallingConventions.Standard && callingConvention != CallingConventions.VarArgs) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); - + throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags); + // vararg is not supported at the moment if (callingConvention == CallingConventions.VarArgs) - throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags")); + throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags); } // We create a transparent assembly to host DynamicMethods. Since the assembly does not have any // non-public fields (or any fields at all), it is a safe anonymous assembly to host DynamicMethods - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable + [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod private static RuntimeModule GetDynamicMethodsModule() { if (s_anonymouslyHostedDynamicMethodsModule != null) @@ -259,7 +245,7 @@ namespace System.Reflection.Emit return s_anonymouslyHostedDynamicMethodsModule; ConstructorInfo transparencyCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); - CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, EmptyArray<Object>.Value); + CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, Array.Empty<Object>()); List<CustomAttributeBuilder> assemblyAttributes = new List<CustomAttributeBuilder>(); assemblyAttributes.Add(transparencyAttribute); @@ -283,38 +269,40 @@ 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) + bool transparentMethod) { 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) - throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); + for (int i = 0; i < signature.Length; i++) + { + if (signature[i] == null) + throw new ArgumentException(SR.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) ) - throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature")); + if (m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void)) + throw new ArgumentException(SR.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 ) - throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType")); + if ((m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef) + throw new NotSupportedException(SR.Arg_InvalidTypeInRetType); if (transparentMethod) { @@ -324,11 +312,10 @@ namespace System.Reflection.Emit { m_restrictedSkipVisibility = true; } - } else { - Debug.Assert(m != null || owner != null, "PerformSecurityCheck should ensure that either m or owner is set"); + Debug.Assert(m != null || owner != null, "Constructor should ensure that either m or owner is set"); Debug.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly"); Debug.Assert(m == null || owner == null, "m and owner cannot both be set"); @@ -344,7 +331,7 @@ namespace System.Reflection.Emit { if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters || rtOwner.IsGenericParameter || rtOwner.IsInterface) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod")); + throw new ArgumentException(SR.Argument_InvalidTypeForDynamicMethod); m_typeOwner = rtOwner; m_module = rtOwner.GetRuntimeModule(); @@ -359,41 +346,18 @@ namespace System.Reflection.Emit m_fInitLocals = true; m_methodHandle = null; - if (name == null) + if (name == null) throw new ArgumentNullException(nameof(name)); -#if FEATURE_APPX - if (AppDomain.ProfileAPICheck) - { - if (m_creatorAssembly == null) - m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark); - - if (m_creatorAssembly != null && !m_creatorAssembly.IsFrameworkAssembly()) - m_profileAPICheck = true; - } -#endif // FEATURE_APPX - m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention); } - private void PerformSecurityCheck(Module m, ref StackCrawlMark stackMark, bool skipVisibility) - { - if (m == null) - throw new ArgumentNullException(nameof(m)); - Contract.EndContractBlock(); - } - - private void PerformSecurityCheck(Type owner, ref StackCrawlMark stackMark, bool skipVisibility) - { - if (owner == null) - throw new ArgumentNullException(nameof(owner)); - } - // // 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 +371,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 @@ -421,33 +386,22 @@ namespace System.Reflection.Emit return d; } -#if FEATURE_APPX - internal bool ProfileAPICheck + // This is guaranteed to return a valid handle + internal unsafe RuntimeMethodHandle GetMethodDescriptor() { - get - { - return m_profileAPICheck; - } - - [FriendAccessAllowed] - set + if (m_methodHandle == null) { - m_profileAPICheck = value; - } - } -#endif - - // This is guaranteed to return a valid handle - internal unsafe RuntimeMethodHandle GetMethodDescriptor() { - if (m_methodHandle == null) { - lock (this) { - 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)); - + throw new InvalidOperationException(SR.Format(SR.InvalidOperation_BadEmptyMethodBody, Name)); + m_ilGenerator.GetCallableMethod(m_module, this); } } @@ -471,7 +425,7 @@ namespace System.Reflection.Emit public override Module Module { get { return m_dynMethod.Module; } } // we cannot return a MethodHandle because we cannot track it via GC so this method is off limits - public override RuntimeMethodHandle MethodHandle { get { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInDynamicMethod")); } } + public override RuntimeMethodHandle MethodHandle { get { throw new InvalidOperationException(SR.InvalidOperation_NotAllowedInDynamicMethod); } } public override MethodAttributes Attributes { get { return m_dynMethod.Attributes; } } @@ -508,9 +462,10 @@ 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")); + throw new NotSupportedException(SR.NotSupported_CallToVarArg); Contract.EndContractBlock(); // @@ -531,7 +486,7 @@ namespace System.Reflection.Emit int formalCount = sig.Arguments.Length; int actualCount = (parameters != null) ? parameters.Length : 0; if (formalCount != actualCount) - throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt")); + throw new TargetParameterCountException(SR.Arg_ParmCnt); // if we are here we passed all the previous checks. Time to look at the arguments Object retValue = null; @@ -554,7 +509,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,24 +526,27 @@ 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")); + throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence); position--; // it's 1 based. 0 is the return value - - if (position >= 0) { - ParameterInfo[] parameters = m_dynMethod.LoadParameters(); + + if (position >= 0) + { + RuntimeParameterInfo[] parameters = m_dynMethod.LoadParameters(); parameters[position].SetName(parameterName); parameters[position].SetAttributes(attributes); } 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 +557,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,109 +580,125 @@ 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 RuntimeParameterInfo[] 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(SR.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. // If we allowed use of RTDynamicMethod, the creator of the DynamicMethod would // not be able to bound access to the DynamicMethod. Hence, we do not allow // direct use of RTDynamicMethod. - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "this"); + throw new ArgumentException(SR.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; + return Array.Empty<Object>(); } - 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 +727,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 +741,49 @@ namespace System.Reflection.Emit // private implementation // - internal ParameterInfo[] LoadParameters() { - if (m_parameters == null) { + internal RuntimeParameterInfo[] 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++) + RuntimeParameterInfo[] parameters = new RuntimeParameterInfo[parameterTypes.Length]; + 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) { - return EmptyArray<Object>.Value; + Object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit) + { + return Array.Empty<Object>(); } - Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) { - return EmptyArray<Object>.Value; + Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) + { + return Array.Empty<Object>(); } - bool ICustomAttributeProvider.IsDefined (Type attributeType, bool inherit) { + bool ICustomAttributeProvider.IsDefined(Type attributeType, bool inherit) + { return false; } } - } - } - } |