How to get Custom Attribute values for enums?

前端 未结 6 1566
独厮守ぢ
独厮守ぢ 2020-12-05 12:45

I have an enum where each member has a custom attribute applied to it. How can I retrieve the value stored in each attribute?

Right now I do this:

va         


        
6条回答
  •  遥遥无期
    2020-12-05 13:35

    Assuming GPUShaderAttribute:

    [AttributeUsage(AttributeTargets.Field,AllowMultiple =false)]
    public class GPUShaderAttribute: Attribute
    {
        public GPUShaderAttribute(string value)
        {
            Value = value;
        }
        public string Value { get; internal set; }
    }
    

    Then we could write a few generic methods to return a dictionary of the enum values and the GPUShaderAttribute object.

        /// 
        /// returns the attribute for a given enum
        ///         
        public static TAttribute GetAttribute(IConvertible @enum)
        {
            TAttribute attributeValue = default(TAttribute);
            if (@enum != null)
            {
                FieldInfo fi = @enum.GetType().GetField(@enum.ToString());
                attributeValue = fi == null ? attributeValue : (TAttribute)fi.GetCustomAttributes(typeof(TAttribute), false).DefaultIfEmpty(null).FirstOrDefault();
    
            }
            return attributeValue;
        }
    

    Then return the whole set with this method.

    /// 
    /// Returns a dictionary of all the Enum fields with the attribute.
    /// 
    public static Dictionary GetEnumObjReference()
    {
        Dictionary _dict = new Dictionary();
        Type enumType = typeof(TEnum);
        Type enumUnderlyingType = Enum.GetUnderlyingType(enumType);
        Array enumValues = Enum.GetValues(enumType);
        foreach (Enum enumValue in enumValues)
        {
            _dict.Add(enumValue, GetAttribute(enumValue));
        }
    
        return _dict;
    }
    

    If you just wanted a string value I would recommend a slightly different route.

        /// 
        /// Returns the string value of the custom attribute property requested.
        /// 
        public static string GetAttributeValue(IConvertible @enum, string propertyName = "Value")
        {
            TAttribute attribute = GetAttribute(@enum);
            return attribute == null ? null : attribute.GetType().GetProperty(propertyName).GetValue(attribute).ToString();
    
        }
    
        /// 
        /// Returns a dictionary of all the Enum fields with the string of the property from the custom attribute nulls default to the enumName
        /// 
        public static Dictionary GetEnumStringReference(string propertyName = "Value")
        {
            Dictionary _dict = new Dictionary();
            Type enumType = typeof(TEnum);
            Type enumUnderlyingType = Enum.GetUnderlyingType(enumType);
            Array enumValues = Enum.GetValues(enumType);
            foreach (Enum enumValue in enumValues)
            {
                string enumName = Enum.GetName(typeof(TEnum), enumValue);
                string decoratorValue = Common.GetAttributeValue(enumValue, propertyName) ?? enumName;
                _dict.Add(enumValue, decoratorValue);
            }
    
            return _dict;
        }
    

提交回复
热议问题