GetAttributeUsage() static private method

static private GetAttributeUsage ( RuntimeType decoratedAttribute ) : AttributeUsageAttribute
decoratedAttribute RuntimeType
return System.AttributeUsageAttribute
        private static bool AttributeUsageCheck(RuntimeType attributeType, bool mustBeInheritable, object[] attributes, IList derivedAttributes)
        {
            AttributeUsageAttribute attributeUsageAttribute = null;

            if (mustBeInheritable)
            {
                attributeUsageAttribute = CustomAttribute.GetAttributeUsage(attributeType);
                if (!attributeUsageAttribute.Inherited)
                {
                    return(false);
                }
            }
            if (derivedAttributes == null)
            {
                return(true);
            }
            for (int i = 0; i < derivedAttributes.Count; i++)
            {
                if (derivedAttributes[i].GetType() == attributeType)
                {
                    if (attributeUsageAttribute == null)
                    {
                        attributeUsageAttribute = CustomAttribute.GetAttributeUsage(attributeType);
                    }
                    return(attributeUsageAttribute.AllowMultiple);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        static internal bool IsDefined(MemberInfo member, Type caType, bool inherit)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            bool isDefined = CustomAttribute.IsDefined(member, caType);

            if (isDefined)
            {
                return(true);
            }

            if (inherit)
            {
                AttributeUsageAttribute usage = CustomAttribute.GetAttributeUsage(caType);
                if (!usage.Inherited)
                {
                    return(false); // kill the inheritance request, the attribute does not allow for it
                }
                // walk up the inheritance chain and look for the specified type
                switch (member.MemberType)
                {
                case MemberTypes.Method:
                {
                    RuntimeMethodInfo baseMethod = ((MethodInfo)member).GetParentDefinition() as RuntimeMethodInfo;
                    while (baseMethod != null)
                    {
                        isDefined = CustomAttribute.IsDefined(baseMethod, caType);
                        if (isDefined)
                        {
                            return(true);
                        }
                        baseMethod = baseMethod.GetParentDefinition() as RuntimeMethodInfo;
                    }
                    break;
                }

                case MemberTypes.TypeInfo:
                case MemberTypes.NestedType:
                {
                    RuntimeType baseType = ((Type)member).BaseType as RuntimeType;
                    while (baseType != null)
                    {
                        isDefined = CustomAttribute.IsDefined((MemberInfo)baseType, caType);
                        if (isDefined)
                        {
                            return(true);
                        }
                        baseType = ((Type)baseType).BaseType as RuntimeType;
                    }
                    break;
                }
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        static internal Object[] GetCustomAttributes(MemberInfo member, Type caType, bool inherit)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            CustomAttribute caItem = GetCustomAttributeList(member, caType, null, 0);

            // if we are asked to go up the hierarchy chain we have to do it now and regardless of the
            // attribute usage for the specific attribute because a derived attribute may ovveride the usage...

            // ... however if the attribute is sealed we can rely on the attribute usage
            if (caType != null && caType.IsSealed && inherit)
            {
                AttributeUsageAttribute usage = CustomAttribute.GetAttributeUsage(caType);
                if (!usage.Inherited)
                {
                    inherit = false; // kill the inheritance request, the attribute does not allow for it
                }
            }

            if (inherit)
            {
                // walk up the inheritance chain and keep accumulating attributes
                int level = 1;
                switch (member.MemberType)
                {
                case MemberTypes.Method:
                {
                    RuntimeMethodInfo baseMethod = ((MethodInfo)member).GetParentDefinition() as RuntimeMethodInfo;
                    while (baseMethod != null)
                    {
                        caItem     = GetCustomAttributeList(baseMethod, caType, caItem, level++);
                        baseMethod = baseMethod.GetParentDefinition() as RuntimeMethodInfo;
                    }
                    break;
                }

                case MemberTypes.TypeInfo:
                case MemberTypes.NestedType:
                {
                    RuntimeType baseType = ((Type)member).BaseType as RuntimeType;
                    while (baseType != null)
                    {
                        caItem   = GetCustomAttributeList((MemberInfo)baseType, caType, caItem, level++);
                        baseType = ((Type)baseType).BaseType as RuntimeType;
                    }
                    break;
                }
                }
            }
            return(CustomAttribute.CheckConsistencyAndCreateArray(caItem, caType));
        }
 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);
     }
 }
Exemplo n.º 5
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);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        // does some consisitency check between the CA list and every CA attribute usage and after
        // discarding unwanted CA return an array of them
        static private Object[] CheckConsistencyAndCreateArray(CustomAttribute caItem, Type caType)
        {
            // we got the max number of attributes, we may have to trim this list but let's get a count for now
            if (caItem == null)
            {
                return((caType == null || caType.IsValueType) ? s_gObjectEmptyArray : (Object[])Array.CreateInstance(caType, 0));
            }

            int             caCount      = 0;
            int             hasInherited = (caItem == null) ? 0 : caItem.m_inheritLevel;
            CustomAttribute caNext       = caItem;
            CustomAttribute caPrev       = null;

            // walk and revert the list
            while (caNext != null)
            {
                caCount++;
                CustomAttribute caTemp = caNext.m_next;
                caNext.m_next = caPrev;
                caPrev        = caNext;
                caNext        = caTemp;
            }
            caItem = caPrev;

            // now we have a list of custom attribute of some type. That list may contain subtype of the specific
            // type, so for every other type we have to go and check whether multiple and inherited are respected
            if (caCount > 0 && hasInherited > 0)
            {
                // keep track of the CA types so we can make inspection reasonable fast
                Hashtable htCAData = new Hashtable(11);

                // loop through the list of attributes checking for consistency, once an attribute
                // has been inspected is never going to be looked at again, and neither an attribute
                // of the same type.
                // This loop and the inspectedCATypes array should make that happen
                caNext = caItem;
                CustomAttribute caLast = null;
                while (caNext != null)
                {
                    AttributeUsageAttribute usage = null;
                    int caTypeLevelFound          = 0;

                    // get current attribute type
                    Type t = caNext.GetAttributeType();

                    // look if the type has already been seen
                    CAData caData = (CAData)htCAData[t];
                    if (caData != null)
                    {
                        usage            = caData.usage;
                        caTypeLevelFound = caData.level;
                    }

                    if (usage == null)
                    {
                        // no type found, load the attribute usage
                        // do not save the attribute usage yet because the next block may discard
                        // the attribute which implies we pretend we never saw. That is kind of
                        // bad because we may end up reloading it again if another type comes into the picture
                        // but we prefer the perf hit to more code complication for a case that seem to
                        // be submarginal
                        usage = CustomAttribute.GetAttributeUsage(t);
                    }

                    // if this is an inherited attribute and the attribute usage does not allow that, OR
                    // if the attribute was seen already on a different inheritance level and multiple is not allowed
                    // THEN discard
                    if ((caNext.m_inheritLevel > 0 && !usage.Inherited) ||
                        (caData != null && caTypeLevelFound < caNext.m_inheritLevel && !usage.AllowMultiple))
                    {
                        if (caLast == null)
                        {
                            caItem = caNext.m_next;
                        }
                        else
                        {
                            caLast.m_next = caNext.m_next;
                        }
                        caNext = caNext.m_next;
                        caCount--;
                        continue;
                    }

                    if (caData == null)
                    {
                        // the attribute was never seen so far so we should add it to the table and keep going
                        htCAData[t] = new CAData(t, usage, caNext.m_inheritLevel);
                    }

                    caLast = caNext;
                    caNext = caNext.m_next;
                }
            }

            // time to return the array
            if (caCount > 0)
            {
                if (caType == null || caType.IsValueType)
                {
                    caType = s_ObjectType;
                }
                Object[] cas = (Object[])Array.CreateInstance(caType, caCount);
                caNext = caItem;
                for (int i = 0; i < caCount; i++)
                {
                    cas[i] = caNext.GetObject();
                    caNext = caNext.m_next;
                }
                return(cas);
            }
            else
            {
                return((caType == null || caType.IsValueType) ? s_gObjectEmptyArray : (Object[])Array.CreateInstance(caType, 0));
            }
        }
Exemplo n.º 9
0
 private static void VerifyPseudoCustomAttribute(RuntimeType pca)
 {
     AttributeUsageAttribute attributeUsage = CustomAttribute.GetAttributeUsage(pca);
 }
Exemplo n.º 10
0
 private static void VerifyPseudoCustomAttribute(Type pca)
 {
     CustomAttribute.GetAttributeUsage(pca as RuntimeType);
 }