internal static object[] GetCustomAttributes(RuntimeModule module, RuntimeType caType)
        {
            int num = 0;

            Attribute[] customAttributes = PseudoCustomAttribute.GetCustomAttributes(module, caType, out num);
            bool        isDecoratedTargetSecurityTransparent = module.GetRuntimeAssembly().IsAllSecurityTransparent();

            object[] customAttributes2 = CustomAttribute.GetCustomAttributes(module, module.MetadataToken, num, caType, isDecoratedTargetSecurityTransparent);
            if (num > 0)
            {
                Array.Copy(customAttributes, 0, customAttributes2, customAttributes2.Length - num, num);
            }
            return(customAttributes2);
        }
        internal static object[] GetCustomAttributes(RuntimeModule module, RuntimeType caType)
        {
            int count = 0;

            Attribute[] sourceArray = PseudoCustomAttribute.GetCustomAttributes(module, caType, out count);
            bool        isDecoratedTargetSecurityTransparent = module.GetRuntimeAssembly().IsAllSecurityTransparent();

            object[] destinationArray = GetCustomAttributes(module, module.MetadataToken, count, caType, isDecoratedTargetSecurityTransparent);
            if (count > 0)
            {
                Array.Copy(sourceArray, 0, destinationArray, destinationArray.Length - count, count);
            }
            return(destinationArray);
        }
Example #3
0
        internal static object[] GetCustomAttributes(RuntimeModule module, RuntimeType caType)
        {
            int count = 0;

            Attribute[]   customAttributes1 = PseudoCustomAttribute.GetCustomAttributes(module, caType, out count);
            bool          flag                = module.GetRuntimeAssembly().IsAllSecurityTransparent();
            RuntimeModule decoratedModule     = module;
            int           metadataToken       = decoratedModule.MetadataToken;
            int           pcaCount            = count;
            RuntimeType   attributeFilterType = caType;
            int           num = flag ? 1 : 0;

            object[] customAttributes2 = CustomAttribute.GetCustomAttributes(decoratedModule, metadataToken, pcaCount, attributeFilterType, num != 0);
            if (count > 0)
            {
                Array.Copy((Array)customAttributes1, 0, (Array)customAttributes2, customAttributes2.Length - count, count);
            }
            return(customAttributes2);
        }
Example #4
0
        private unsafe static object[] GetCustomAttributes(
            RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, 
            RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            Contract.EndContractBlock();

            MetadataImport scope = decoratedModule.MetadataImport;
            CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);

            bool useObjectArray = (attributeFilterType == null || attributeFilterType.IsValueType || attributeFilterType.ContainsGenericParameters);
            Type arrayType = useObjectArray ? typeof(object) : attributeFilterType;

            if (attributeFilterType == null && car.Length == 0)
                return CreateAttributeArrayHelper(arrayType, 0);

            object[] attributes = CreateAttributeArrayHelper(arrayType, car.Length);
            int cAttributes = 0;

            // Custom attribute security checks are done with respect to the assembly *decorated* with the 
            // custom attribute as opposed to the *caller of GetCustomAttributes*.
            // Since this assembly might not be on the stack and the attribute ctor or property setters we're about to invoke may
            // make security demands, we push a frame on the stack as a proxy for the decorated assembly (this frame will be picked
            // up an interpreted by the security stackwalker).
            // Once we push the frame it will be automatically popped in the event of an exception, so no need to use CERs or the
            // like.
            SecurityContextFrame frame = new SecurityContextFrame();
            frame.Push(decoratedModule.GetRuntimeAssembly());

            // Optimization for the case where attributes decorate entities in the same assembly in which case 
            // we can cache the successful APTCA check between the decorated and the declared assembly.
            Assembly lastAptcaOkAssembly = null;

            for (int i = 0; i < car.Length; i++)
            {
                object attribute = null;
                CustomAttributeRecord caRecord = car[i];

                IRuntimeMethodInfo ctor = null;
                RuntimeType attributeType = null;
                bool ctorHasParameters, isVarArg;
                int cNamedArgs = 0;

                IntPtr blobStart = caRecord.blob.Signature;
                IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);
                int blobLen = (int)((byte*)blobEnd - (byte*)blobStart);

                if (!FilterCustomAttributeRecord(caRecord, scope, ref lastAptcaOkAssembly, 
                                                 decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, 
                                                 attributes, derivedAttributes,
                                                 out attributeType, out ctor, out ctorHasParameters, out isVarArg))
                    continue;

                if (ctor != null)
                {
                    // Linktime demand checks 
                    // decoratedMetadataToken needed as it may be "transparent" in which case we do a full stack walk
                    RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                }
                else
                {

                }

                // Leverage RuntimeConstructorInfo standard .ctor verfication
                RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, isVarArg); 

                // Create custom attribute object
                if (ctorHasParameters)
                {
                    attribute = CreateCaObject(decoratedModule, ctor, ref blobStart, blobEnd, out cNamedArgs); 
                }
                else
                {
                    attribute = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);

                    // It is allowed by the ECMA spec to have an empty signature blob
                    if (blobLen == 0)
                        cNamedArgs = 0;
                    else
                    {
                        // Metadata is always written in little-endian format. Must account for this on
                        // big-endian platforms.
#if BIGENDIAN
                        const int CustomAttributeVersion = 0x0100;
#else
                        const int CustomAttributeVersion = 0x0001;
#endif
                        if (Marshal.ReadInt16(blobStart) != CustomAttributeVersion)
                            throw new CustomAttributeFormatException();
                        blobStart = (IntPtr)((byte*)blobStart + 2); // skip version prefix

                        cNamedArgs = Marshal.ReadInt16(blobStart);
                        blobStart = (IntPtr)((byte*)blobStart + 2); // skip namedArgs count
#if BIGENDIAN
                        cNamedArgs = ((cNamedArgs & 0xff00) >> 8) | ((cNamedArgs & 0x00ff) << 8);
#endif
                    }
                }

                for (int j = 0; j < cNamedArgs; j++)
                {
                    #region // Initialize named properties and fields
                    string name;
                    bool isProperty;
                    RuntimeType type;
                    object value;
                    
                    IntPtr blobItr = caRecord.blob.Signature;

                    GetPropertyOrFieldData(decoratedModule, ref blobStart, blobEnd, out name, out isProperty, out type, out value);

                    try
                    {
                        if (isProperty)
                        {
                            #region // Initialize property
                            if (type == null && value != null)
                            {
                                type = (RuntimeType)value.GetType();
                                if (type == Type_RuntimeType)
                                    type = Type_Type;
                            }

                            RuntimePropertyInfo property = null;

                            if (type == null)
                                property = attributeType.GetProperty(name) as RuntimePropertyInfo;
                            else
                                property = attributeType.GetProperty(name, type, Type.EmptyTypes) as RuntimePropertyInfo;

                            // Did we get a valid property reference?
                            if (property == null)
                            {
                                throw new CustomAttributeFormatException(
                                    String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(
                                        isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), name));
                            }

                            RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo;
                            
                            // Public properties may have non-public setter methods
                            if (!setMethod.IsPublic)
                                continue;

                            RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent);

                            setMethod.UnsafeInvoke(attribute, BindingFlags.Default, null, new object[] { value }, null);
                            #endregion
                        }
                        else
                        {
                            RtFieldInfo field = attributeType.GetField(name) as RtFieldInfo;

                            if (isDecoratedTargetSecurityTransparent)
                            {
                                RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle());
                            }

                            field.CheckConsistency(attribute);
                            field.UnsafeSetValue(attribute, value, BindingFlags.Default, Type.DefaultBinder, null);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new CustomAttributeFormatException(
                            String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(
                                isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), name), e);
                    }
                    #endregion
                }

                if (!blobStart.Equals(blobEnd))
                    throw new CustomAttributeFormatException();

                attributes[cAttributes++] = attribute;
            }

            // The frame will be popped automatically if we take an exception any time after we pushed it. So no need of a catch or
            // finally or CERs here.
            frame.Pop();

            if (cAttributes == car.Length && pcaCount == 0)
                return attributes;

            object[] result = CreateAttributeArrayHelper(arrayType, cAttributes + pcaCount);
            Array.Copy(attributes, 0, result, 0, cAttributes);
            return result;
        }
Example #5
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        internal static Object[] GetCustomAttributes(RuntimeModule module, RuntimeType caType) 
        {
            Contract.Requires(module != null);
            Contract.Requires(caType != null);

            int pcaCount = 0;
            Attribute[] pca = PseudoCustomAttribute.GetCustomAttributes(module, caType, out pcaCount);
            bool isModuleSecurityTransparent = module.GetRuntimeAssembly().IsAllSecurityTransparent();
            object[] attributes = GetCustomAttributes(module, module.MetadataToken, pcaCount, caType, isModuleSecurityTransparent);
            if (pcaCount > 0) Array.Copy(pca, 0, attributes, attributes.Length - pcaCount, pcaCount);
            return attributes;
        }
Example #6
0
        private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            }
            MetadataImport metadataImport = decoratedModule.MetadataImport;

            CustomAttributeRecord[] attributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
            Type elementType = (attributeFilterType == (RuntimeType)null || attributeFilterType.IsValueType ? 1 : (attributeFilterType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)attributeFilterType;

            if (attributeFilterType == (RuntimeType)null && attributeRecords.Length == 0)
            {
                return(CustomAttribute.CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             attributeArrayHelper1 = CustomAttribute.CreateAttributeArrayHelper(elementType, attributeRecords.Length);
            int                  length = 0;
            SecurityContextFrame securityContextFrame = new SecurityContextFrame();

            securityContextFrame.Push(decoratedModule.GetRuntimeAssembly());
            Assembly lastAptcaOkAssembly = (Assembly)null;

            for (int index1 = 0; index1 < attributeRecords.Length; ++index1)
            {
                CustomAttributeRecord caRecord      = attributeRecords[index1];
                IRuntimeMethodInfo    ctor          = (IRuntimeMethodInfo)null;
                RuntimeType           attributeType = (RuntimeType)null;
                int    namedArgs = 0;
                IntPtr ptr1      = caRecord.blob.Signature;
                IntPtr blobEnd   = (IntPtr)((void *)((IntPtr)(void *)ptr1 + caRecord.blob.Length));
                int    num       = (int)((sbyte *)(void *)blobEnd - (sbyte *)(void *)ptr1);
                bool   ctorHasParameters;
                bool   isVarArg;
                if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, (MetadataToken)decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributeArrayHelper1, derivedAttributes, out attributeType, out ctor, out ctorHasParameters, out isVarArg))
                {
                    if (ctor != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance((Type)attributeType, isVarArg);
                    object target;
                    if (ctorHasParameters)
                    {
                        target = CustomAttribute.CreateCaObject(decoratedModule, ctor, ref ptr1, blobEnd, out namedArgs);
                    }
                    else
                    {
                        target = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                        if (num == 0)
                        {
                            namedArgs = 0;
                        }
                        else
                        {
                            if ((int)Marshal.ReadInt16(ptr1) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            IntPtr ptr2 = (IntPtr)((void *)((IntPtr)(void *)ptr1 + 2));
                            namedArgs = (int)Marshal.ReadInt16(ptr2);
                            ptr1      = (IntPtr)((void *)((IntPtr)(void *)ptr2 + 2));
                        }
                    }
                    for (int index2 = 0; index2 < namedArgs; ++index2)
                    {
                        IntPtr      signature = caRecord.blob.Signature;
                        string      name;
                        bool        isProperty;
                        RuntimeType type;
                        object      obj;
                        CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref ptr1, blobEnd, out name, out isProperty, out type, out obj);
                        try
                        {
                            if (isProperty)
                            {
                                if (type == (RuntimeType)null && obj != null)
                                {
                                    type = (RuntimeType)obj.GetType();
                                    if (type == CustomAttribute.Type_RuntimeType)
                                    {
                                        type = CustomAttribute.Type_Type;
                                    }
                                }
                                RuntimePropertyInfo runtimePropertyInfo = !(type == (RuntimeType)null) ? attributeType.GetProperty(name, (Type)type, Type.EmptyTypes) as RuntimePropertyInfo : attributeType.GetProperty(name) as RuntimePropertyInfo;
                                if ((PropertyInfo)runtimePropertyInfo == (PropertyInfo)null)
                                {
                                    throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name));
                                }
                                RuntimeMethodInfo runtimeMethodInfo = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo;
                                if (runtimeMethodInfo.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands((IRuntimeMethodInfo)runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    runtimeMethodInfo.UnsafeInvoke(target, BindingFlags.Default, (Binder)null, new object[1] {
                                        obj
                                    }, (CultureInfo)null);
                                }
                            }
                            else
                            {
                                RtFieldInfo rtFieldInfo = attributeType.GetField(name) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                rtFieldInfo.CheckConsistency(target);
                                rtFieldInfo.UnsafeSetValue(target, obj, BindingFlags.Default, Type.DefaultBinder, (CultureInfo)null);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name), ex);
                        }
                    }
                    if (!ptr1.Equals((object)blobEnd))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    attributeArrayHelper1[length++] = target;
                }
            }
            securityContextFrame.Pop();
            if (length == attributeRecords.Length && pcaCount == 0)
            {
                return(attributeArrayHelper1);
            }
            object[] attributeArrayHelper2 = CustomAttribute.CreateAttributeArrayHelper(elementType, length + pcaCount);
            Array.Copy((Array)attributeArrayHelper1, 0, (Array)attributeArrayHelper2, 0, length);
            return(attributeArrayHelper2);
        }
        private unsafe static object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            }
            MetadataImport metadataImport = decoratedModule.MetadataImport;

            CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
            Type elementType = (attributeFilterType == null || attributeFilterType.IsValueType || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType;

            if (attributeFilterType == null && customAttributeRecords.Length == 0)
            {
                return(CustomAttribute.CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             array = CustomAttribute.CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
            int                  num   = 0;
            SecurityContextFrame securityContextFrame = default(SecurityContextFrame);

            securityContextFrame.Push(decoratedModule.GetRuntimeAssembly());
            Assembly assembly = null;

            for (int i = 0; i < customAttributeRecords.Length; i++)
            {
                object obj = null;
                CustomAttributeRecord caRecord          = customAttributeRecords[i];
                IRuntimeMethodInfo    runtimeMethodInfo = null;
                RuntimeType           runtimeType       = null;
                int    num2    = 0;
                IntPtr intPtr  = caRecord.blob.Signature;
                IntPtr intPtr2 = (IntPtr)((void *)((byte *)((void *)intPtr) + caRecord.blob.Length));
                int    num3    = (int)((long)((byte *)((void *)intPtr2) - (byte *)((void *)intPtr)));
                bool   flag;
                bool   isVarArg;
                if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref assembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, array, derivedAttributes, out runtimeType, out runtimeMethodInfo, out flag, out isVarArg))
                {
                    if (runtimeMethodInfo != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance(runtimeType, isVarArg);
                    if (flag)
                    {
                        obj = CustomAttribute.CreateCaObject(decoratedModule, runtimeMethodInfo, ref intPtr, intPtr2, out num2);
                    }
                    else
                    {
                        obj = RuntimeTypeHandle.CreateCaInstance(runtimeType, runtimeMethodInfo);
                        if (num3 == 0)
                        {
                            num2 = 0;
                        }
                        else
                        {
                            if (Marshal.ReadInt16(intPtr) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2));
                            num2   = (int)Marshal.ReadInt16(intPtr);
                            intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2));
                        }
                    }
                    for (int j = 0; j < num2; j++)
                    {
                        IntPtr      signature = caRecord.blob.Signature;
                        string      text;
                        bool        flag2;
                        RuntimeType runtimeType2;
                        object      obj2;
                        CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref intPtr, intPtr2, out text, out flag2, out runtimeType2, out obj2);
                        try
                        {
                            if (flag2)
                            {
                                if (runtimeType2 == null && obj2 != null)
                                {
                                    runtimeType2 = (RuntimeType)obj2.GetType();
                                    if (runtimeType2 == CustomAttribute.Type_RuntimeType)
                                    {
                                        runtimeType2 = CustomAttribute.Type_Type;
                                    }
                                }
                                RuntimePropertyInfo runtimePropertyInfo;
                                if (runtimeType2 == null)
                                {
                                    runtimePropertyInfo = (runtimeType.GetProperty(text) as RuntimePropertyInfo);
                                }
                                else
                                {
                                    runtimePropertyInfo = (runtimeType.GetProperty(text, runtimeType2, Type.EmptyTypes) as RuntimePropertyInfo);
                                }
                                if (runtimePropertyInfo == null)
                                {
                                    throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text));
                                }
                                RuntimeMethodInfo runtimeMethodInfo2 = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo;
                                if (runtimeMethodInfo2.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo2, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    runtimeMethodInfo2.UnsafeInvoke(obj, BindingFlags.Default, null, new object[]
                                    {
                                        obj2
                                    }, null);
                                }
                            }
                            else
                            {
                                RtFieldInfo rtFieldInfo = runtimeType.GetField(text) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                rtFieldInfo.CheckConsistency(obj);
                                rtFieldInfo.UnsafeSetValue(obj, obj2, BindingFlags.Default, Type.DefaultBinder, null);
                            }
                        }
                        catch (Exception inner)
                        {
                            throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text), inner);
                        }
                    }
                    if (!intPtr.Equals(intPtr2))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    array[num++] = obj;
                }
            }
            securityContextFrame.Pop();
            if (num == customAttributeRecords.Length && pcaCount == 0)
            {
                return(array);
            }
            object[] array2 = CustomAttribute.CreateAttributeArrayHelper(elementType, num + pcaCount);
            Array.Copy(array, 0, array2, 0, num);
            return(array2);
        }
        private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            }
            MetadataImport metadataImport = decoratedModule.MetadataImport;

            CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
            Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType;

            if ((attributeFilterType == null) && (customAttributeRecords.Length == 0))
            {
                return(CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             attributes = CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
            int                  length     = 0;
            SecurityContextFrame frame      = new SecurityContextFrame();

            frame.Push(decoratedModule.GetRuntimeAssembly());
            Assembly lastAptcaOkAssembly = null;

            for (int i = 0; i < customAttributeRecords.Length; i++)
            {
                bool   flag2;
                bool   flag3;
                object obj2 = null;
                CustomAttributeRecord caRecord      = customAttributeRecords[i];
                IRuntimeMethodInfo    ctor          = null;
                RuntimeType           attributeType = null;
                int    namedArgs = 0;
                IntPtr signature = caRecord.blob.Signature;
                IntPtr blobEnd   = (IntPtr)(((void *)signature) + caRecord.blob.Length);
                int    num4      = (int)((long)((((void *)blobEnd) - ((void *)signature)) / 1));
                if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3))
                {
                    if (ctor != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3);
                    if (flag2)
                    {
                        obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs);
                    }
                    else
                    {
                        obj2 = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                        if (num4 == 0)
                        {
                            namedArgs = 0;
                        }
                        else
                        {
                            if (Marshal.ReadInt16(signature) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            signature = (IntPtr)(((void *)signature) + 2);
                            namedArgs = Marshal.ReadInt16(signature);
                            signature = (IntPtr)(((void *)signature) + 2);
                        }
                    }
                    for (int j = 0; j < namedArgs; j++)
                    {
                        string      str;
                        bool        flag4;
                        RuntimeType type;
                        object      obj3;
                        IntPtr      ptr1 = caRecord.blob.Signature;
                        GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type, out obj3);
                        try
                        {
                            if (flag4)
                            {
                                if ((type == null) && (obj3 != null))
                                {
                                    type = (RuntimeType)obj3.GetType();
                                    if (type == Type_RuntimeType)
                                    {
                                        type = Type_Type;
                                    }
                                }
                                RuntimePropertyInfo property = null;
                                if (type == null)
                                {
                                    property = attributeType.GetProperty(str) as RuntimePropertyInfo;
                                }
                                else
                                {
                                    property = attributeType.GetProperty(str, type, Type.EmptyTypes) as RuntimePropertyInfo;
                                }
                                if (property == null)
                                {
                                    throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }));
                                }
                                RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo;
                                if (setMethod.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true);
                                }
                            }
                            else
                            {
                                RtFieldInfo field = attributeType.GetField(str) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                field.InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false);
                            }
                        }
                        catch (Exception exception)
                        {
                            throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception);
                        }
                    }
                    if (!signature.Equals(blobEnd))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    attributes[length++] = obj2;
                }
            }
            frame.Pop();
            if ((length == customAttributeRecords.Length) && (pcaCount == 0))
            {
                return(attributes);
            }
            object[] destinationArray = CreateAttributeArrayHelper(elementType, length + pcaCount);
            Array.Copy(attributes, 0, destinationArray, 0, length);
            return(destinationArray);
        }
 private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
 {
     if (decoratedModule.Assembly.ReflectionOnly)
     {
         throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
     }
     MetadataImport metadataImport = decoratedModule.MetadataImport;
     CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
     Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType;
     if ((attributeFilterType == null) && (customAttributeRecords.Length == 0))
     {
         return CreateAttributeArrayHelper(elementType, 0);
     }
     object[] attributes = CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
     int length = 0;
     SecurityContextFrame frame = new SecurityContextFrame();
     frame.Push(decoratedModule.GetRuntimeAssembly());
     Assembly lastAptcaOkAssembly = null;
     for (int i = 0; i < customAttributeRecords.Length; i++)
     {
         bool flag2;
         bool flag3;
         object obj2 = null;
         CustomAttributeRecord caRecord = customAttributeRecords[i];
         IRuntimeMethodInfo ctor = null;
         RuntimeType attributeType = null;
         int namedArgs = 0;
         IntPtr signature = caRecord.blob.Signature;
         IntPtr blobEnd = (IntPtr) (((void*) signature) + caRecord.blob.Length);
         int num4 = (int) ((long) ((((void*) blobEnd) - ((void*) signature)) / 1));
         if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3))
         {
             if (ctor != null)
             {
                 RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
             }
             RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3);
             if (flag2)
             {
                 obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs);
             }
             else
             {
                 obj2 = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                 if (num4 == 0)
                 {
                     namedArgs = 0;
                 }
                 else
                 {
                     if (Marshal.ReadInt16(signature) != 1)
                     {
                         throw new CustomAttributeFormatException();
                     }
                     signature = (IntPtr) (((void*) signature) + 2);
                     namedArgs = Marshal.ReadInt16(signature);
                     signature = (IntPtr) (((void*) signature) + 2);
                 }
             }
             for (int j = 0; j < namedArgs; j++)
             {
                 string str;
                 bool flag4;
                 RuntimeType type;
                 object obj3;
                 IntPtr ptr1 = caRecord.blob.Signature;
                 GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type, out obj3);
                 try
                 {
                     if (flag4)
                     {
                         if ((type == null) && (obj3 != null))
                         {
                             type = (RuntimeType) obj3.GetType();
                             if (type == Type_RuntimeType)
                             {
                                 type = Type_Type;
                             }
                         }
                         RuntimePropertyInfo property = null;
                         if (type == null)
                         {
                             property = attributeType.GetProperty(str) as RuntimePropertyInfo;
                         }
                         else
                         {
                             property = attributeType.GetProperty(str, type, Type.EmptyTypes) as RuntimePropertyInfo;
                         }
                         if (property == null)
                         {
                             throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }));
                         }
                         RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo;
                         if (setMethod.IsPublic)
                         {
                             RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent);
                             setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true);
                         }
                     }
                     else
                     {
                         RtFieldInfo field = attributeType.GetField(str) as RtFieldInfo;
                         if (isDecoratedTargetSecurityTransparent)
                         {
                             RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle());
                         }
                         field.InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false);
                     }
                 }
                 catch (Exception exception)
                 {
                     throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception);
                 }
             }
             if (!signature.Equals(blobEnd))
             {
                 throw new CustomAttributeFormatException();
             }
             attributes[length++] = obj2;
         }
     }
     frame.Pop();
     if ((length == customAttributeRecords.Length) && (pcaCount == 0))
     {
         return attributes;
     }
     object[] destinationArray = CreateAttributeArrayHelper(elementType, length + pcaCount);
     Array.Copy(attributes, 0, destinationArray, 0, length);
     return destinationArray;
 }
 internal static object[] GetCustomAttributes(RuntimeModule module, RuntimeType caType)
 {
     int count = 0;
     Attribute[] sourceArray = PseudoCustomAttribute.GetCustomAttributes(module, caType, out count);
     bool isDecoratedTargetSecurityTransparent = module.GetRuntimeAssembly().IsAllSecurityTransparent();
     object[] destinationArray = GetCustomAttributes(module, module.MetadataToken, count, caType, isDecoratedTargetSecurityTransparent);
     if (count > 0)
     {
         Array.Copy(sourceArray, 0, destinationArray, destinationArray.Length - count, count);
     }
     return destinationArray;
 }