System.Reflection.CustomAttribute.GetAttributeUsage C# (CSharp) Method

GetAttributeUsage() static private method

static private GetAttributeUsage ( RuntimeType decoratedAttribute ) : AttributeUsageAttribute
decoratedAttribute RuntimeType
return System.AttributeUsageAttribute
        internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedAttribute)
        {
            Module decoratedModule = decoratedAttribute.Module;
            MetadataImport scope = decoratedModule.MetadataImport;
            CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedAttribute.MetadataToken);

            AttributeUsageAttribute attributeUsageAttribute = null;

            for (int i = 0; i < car.Length; i++)
            {
                CustomAttributeRecord caRecord = car[i];
                RuntimeType attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;

                if (attributeType != typeof(AttributeUsageAttribute))
                    continue;

                if (attributeUsageAttribute != null)
                    throw new FormatException(String.Format(
                        CultureInfo.CurrentUICulture, Environment.GetResourceString("Format_AttributeUsage"), attributeType));

                AttributeTargets targets;
                bool inherited, allowMultiple;
                ParseAttributeUsageAttribute(caRecord.blob, out targets, out inherited, out allowMultiple);
                attributeUsageAttribute = new AttributeUsageAttribute(targets, allowMultiple, inherited);
            }

            if (attributeUsageAttribute == null)
                return AttributeUsageAttribute.Default;

            return attributeUsageAttribute;
        }
        #endregion

Usage Example

        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);
        }
All Usage Examples Of System.Reflection.CustomAttribute::GetAttributeUsage