System.MonoCustomAttrs.RetrieveAttributeUsage C# (CSharp) Метод

RetrieveAttributeUsage() приватный статический Метод

private static RetrieveAttributeUsage ( Type attributeType ) : AttributeUsageAttribute
attributeType Type
Результат AttributeUsageAttribute
		private static AttributeUsageAttribute RetrieveAttributeUsage (Type attributeType)
		{
			if (attributeType == typeof (AttributeUsageAttribute))
				/* Avoid endless recursion */
				return new AttributeUsageAttribute (AttributeTargets.Class);

			AttributeUsageAttribute usageAttribute = null;
			object[] attribs = GetCustomAttributes (attributeType,
				MonoCustomAttrs.AttributeUsageType, false);
			if (attribs.Length == 0)
			{
				// if no AttributeUsage was defined on the attribute level, then
				// try to retrieve if from its base type
				if (attributeType.BaseType != null)
				{
					usageAttribute = RetrieveAttributeUsage (attributeType.BaseType);

				}
				if (usageAttribute != null)
				{
					// return AttributeUsage of base class
					return usageAttribute;

				}
				// return default AttributeUsageAttribute if no AttributeUsage 
				// was defined on attribute, or its base class
				return DefaultAttributeUsage;
			}
			// check if more than one AttributeUsageAttribute has been specified 
			// on the type
			// NOTE: compilers should prevent this, but that doesn't prevent
			// anyone from using IL ofcourse
			if (attribs.Length > 1)
			{
				throw new FormatException ("Duplicate AttributeUsageAttribute cannot be specified on an attribute type.");
			}

			return ((AttributeUsageAttribute) attribs[0]);
		}

Usage Example

Пример #1
0
        private static AttributeUsageAttribute RetrieveAttributeUsage(Type attributeType)
        {
            if (attributeType == typeof(AttributeUsageAttribute))
            {
                return(new AttributeUsageAttribute(AttributeTargets.Class));
            }
            AttributeUsageAttribute attributeUsageAttribute = null;

            object[] customAttributes = MonoCustomAttrs.GetCustomAttributes(attributeType, MonoCustomAttrs.AttributeUsageType, false);
            if (customAttributes.Length == 0)
            {
                if (attributeType.BaseType != null)
                {
                    attributeUsageAttribute = MonoCustomAttrs.RetrieveAttributeUsage(attributeType.BaseType);
                }
                if (attributeUsageAttribute != null)
                {
                    return(attributeUsageAttribute);
                }
                return(MonoCustomAttrs.DefaultAttributeUsage);
            }
            else
            {
                if (customAttributes.Length > 1)
                {
                    throw new FormatException("Duplicate AttributeUsageAttribute cannot be specified on an attribute type.");
                }
                return((AttributeUsageAttribute)customAttributes[0]);
            }
        }
All Usage Examples Of System.MonoCustomAttrs::RetrieveAttributeUsage