internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
 {
     if (type.GetElementType() != null)
     {
         if (!caType.IsValueType)
         {
             return(CustomAttribute.CreateAttributeArrayHelper(caType, 0));
         }
         return(EmptyArray <object> .Value);
     }
     else
     {
         if (type.IsGenericType && !type.IsGenericTypeDefinition)
         {
             type = (type.GetGenericTypeDefinition() as RuntimeType);
         }
         int         i = 0;
         Attribute[] customAttributes = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out i);
         if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
         {
             object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, i, caType, !CustomAttribute.AllowCriticalCustomAttributes(type));
             if (i > 0)
             {
                 Array.Copy(customAttributes, 0, customAttributes2, customAttributes2.Length - i, i);
             }
             return(customAttributes2);
         }
         List <object> list = new List <object>();
         bool          mustBeInheritable = false;
         Type          elementType       = (caType == null || caType.IsValueType || caType.ContainsGenericParameters) ? typeof(object) : caType;
         while (i > 0)
         {
             list.Add(customAttributes[--i]);
         }
         while (type != (RuntimeType)typeof(object) && type != null)
         {
             object[] customAttributes3 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, list, !CustomAttribute.AllowCriticalCustomAttributes(type));
             mustBeInheritable = true;
             for (int j = 0; j < customAttributes3.Length; j++)
             {
                 list.Add(customAttributes3[j]);
             }
             type = (type.BaseType as RuntimeType);
         }
         object[] array = CustomAttribute.CreateAttributeArrayHelper(elementType, list.Count);
         Array.Copy(list.ToArray(), 0, array, 0, list.Count);
         return(array);
     }
 }
        public override object[] GetCustomAttributes(Type attributeType, bool inherit)
        {
            ArgumentNullException.ThrowIfNull(attributeType);

            if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
            {
                throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
            }

            if (MdToken.IsNullToken(m_tkParamDef))
            {
                return(CustomAttribute.CreateAttributeArrayHelper(attributeRuntimeType, 0));
            }

            return(CustomAttribute.GetCustomAttributes(this, attributeRuntimeType));
        }
Esempio n. 3
0
        internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
        {
            if (type.GetElementType() != (Type)null)
            {
                if (!caType.IsValueType)
                {
                    return(CustomAttribute.CreateAttributeArrayHelper((Type)caType, 0));
                }
                return(EmptyArray <object> .Value);
            }
            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                type = type.GetGenericTypeDefinition() as RuntimeType;
            }
            int count = 0;

            Attribute[] customAttributes1 = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out count);
            if (!inherit || caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited)
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, count, caType, !CustomAttribute.AllowCriticalCustomAttributes(type));
                if (count > 0)
                {
                    Array.Copy((Array)customAttributes1, 0, (Array)customAttributes2, customAttributes2.Length - count, count);
                }
                return(customAttributes2);
            }
            List <object> objectList        = new List <object>();
            bool          mustBeInheritable = false;
            Type          elementType       = (caType == (RuntimeType)null || caType.IsValueType ? 1 : (caType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)caType;

            while (count > 0)
            {
                objectList.Add((object)customAttributes1[--count]);
            }
            for (; type != (RuntimeType)typeof(object) && type != (RuntimeType)null; type = type.BaseType as RuntimeType)
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, (IList)objectList, !CustomAttribute.AllowCriticalCustomAttributes(type));
                mustBeInheritable = true;
                for (int index = 0; index < customAttributes2.Length; ++index)
                {
                    objectList.Add(customAttributes2[index]);
                }
            }
            object[] attributeArrayHelper = CustomAttribute.CreateAttributeArrayHelper(elementType, objectList.Count);
            Array.Copy((Array)objectList.ToArray(), 0, (Array)attributeArrayHelper, 0, objectList.Count);
            return(attributeArrayHelper);
        }
        internal static object[] GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
        {
            if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
            {
                method = (method.GetGenericMethodDefinition() as RuntimeMethodInfo);
            }
            int i = 0;

            Attribute[] customAttributes = PseudoCustomAttribute.GetCustomAttributes(method, caType, true, out i);
            if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(method.GetRuntimeModule(), method.MetadataToken, i, caType, !CustomAttribute.AllowCriticalCustomAttributes(method));
                if (i > 0)
                {
                    Array.Copy(customAttributes, 0, customAttributes2, customAttributes2.Length - i, i);
                }
                return(customAttributes2);
            }
            List <object> list = new List <object>();
            bool          mustBeInheritable = false;
            Type          elementType       = (caType == null || caType.IsValueType || caType.ContainsGenericParameters) ? typeof(object) : caType;

            while (i > 0)
            {
                list.Add(customAttributes[--i]);
            }
            while (method != null)
            {
                object[] customAttributes3 = CustomAttribute.GetCustomAttributes(method.GetRuntimeModule(), method.MetadataToken, 0, caType, mustBeInheritable, list, !CustomAttribute.AllowCriticalCustomAttributes(method));
                mustBeInheritable = true;
                for (int j = 0; j < customAttributes3.Length; j++)
                {
                    list.Add(customAttributes3[j]);
                }
                method = method.GetParentDefinition();
            }
            object[] array = CustomAttribute.CreateAttributeArrayHelper(elementType, list.Count);
            Array.Copy(list.ToArray(), 0, array, 0, list.Count);
            return(array);
        }
Esempio n. 5
0
        internal static object[] GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
        {
            if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
            {
                method = method.GetGenericMethodDefinition() as RuntimeMethodInfo;
            }
            int count = 0;

            Attribute[] customAttributes1 = PseudoCustomAttribute.GetCustomAttributes(method, caType, true, out count);
            if (!inherit || caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited)
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(method.GetRuntimeModule(), method.MetadataToken, count, caType, !CustomAttribute.AllowCriticalCustomAttributes((MethodBase)method));
                if (count > 0)
                {
                    Array.Copy((Array)customAttributes1, 0, (Array)customAttributes2, customAttributes2.Length - count, count);
                }
                return(customAttributes2);
            }
            List <object> objectList        = new List <object>();
            bool          mustBeInheritable = false;
            Type          elementType       = (caType == (RuntimeType)null || caType.IsValueType ? 1 : (caType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)caType;

            while (count > 0)
            {
                objectList.Add((object)customAttributes1[--count]);
            }
            for (; (MethodInfo)method != (MethodInfo)null; method = method.GetParentDefinition())
            {
                object[] customAttributes2 = CustomAttribute.GetCustomAttributes(method.GetRuntimeModule(), method.MetadataToken, 0, caType, mustBeInheritable, (IList)objectList, !CustomAttribute.AllowCriticalCustomAttributes((MethodBase)method));
                mustBeInheritable = true;
                for (int index = 0; index < customAttributes2.Length; ++index)
                {
                    objectList.Add(customAttributes2[index]);
                }
            }
            object[] attributeArrayHelper = CustomAttribute.CreateAttributeArrayHelper(elementType, objectList.Count);
            Array.Copy((Array)objectList.ToArray(), 0, (Array)attributeArrayHelper, 0, objectList.Count);
            return(attributeArrayHelper);
        }
Esempio n. 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);
        }