代码之家  ›  专栏  ›  技术社区  ›  Alex

属性的自定义属性-获取属性的类型和值

  •  28
  • Alex  · 技术社区  · 16 年前

    我有以下自定义属性,可以应用于属性:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class IdentifierAttribute : Attribute
    {
    }
    

    例如:

    public class MyClass
    {
        [Identifier()]
        public string Name { get; set; }
    
        public int SomeNumber { get; set; }
        public string SomeOtherProperty { get; set; }
    }
    

    还有其他类,标识符属性可以添加到不同类型的属性中:

    public class MyOtherClass
    {
        public string Name { get; set; }
    
        [Identifier()]
        public int SomeNumber { get; set; }
    
        public string SomeOtherProperty { get; set; }
    }
    

    然后我需要能够在我的消费课上获得这些信息。 例如:

    public class TestClass<T>
    {
        public void GetIDForPassedInObject(T obj)
        {
            var type = obj.GetType();
            //type.GetCustomAttributes(true)???
        }
    }
    

    最好的方法是什么? 我需要得到[identifier()]字段的类型(int、string等…)和实际值,显然是基于类型。

    3 回复  |  直到 8 年前
        1
  •  35
  •   Richard Friend    16 年前

    类似于下面的内容,这将只使用具有属性的Accross的第一个属性,当然您可以将它放在多个属性上。

        public object GetIDForPassedInObject(T obj)
        {
            var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                       .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1);
            object ret = prop !=null ?  prop.GetValue(obj, null) : null;
    
            return ret;
        }  
    
        2
  •  2
  •   this. __curious_geek    16 年前
    public class TestClass<T>
    {
        public void GetIDForPassedInObject(T obj)
        {
            PropertyInfo[] properties =
                obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);            
    
            PropertyInfo IdProperty = (from PropertyInfo property in properties
                               where property.GetCustomAttributes(typeof(Identifier), true).Length > 0
                               select property).First();
    
             if(null == IdProperty)
                 throw new ArgumentException("obj does not have Identifier.");
    
             Object propValue = IdProperty.GetValue(entity, null)
        }
    }
    
        3
  •  2
  •   PmanAce    8 年前

    稍微晚了一点,但我为枚举(也可以是任何对象)做了一些事情,并使用扩展名获取描述属性值(这可能是任何属性的泛型):

    public enum TransactionTypeEnum
    {
        [Description("Text here!")]
        DROP = 1,
    
        [Description("More text here!")]
        PICKUP = 2,
    
        ...
    }
    

    获取值:

    var code = TransactionTypeEnum.DROP.ToCode();
    

    支持所有枚举的扩展名:

    public static string ToCode(this TransactionTypeEnum val)
    {
        return GetCode(val);
    }
    
    public static string ToCode(this DockStatusEnum val)
    {
        return GetCode(val);
    }
    
    public static string ToCode(this TrailerStatusEnum val)
    {
        return GetCode(val);
    }
    
    public static string ToCode(this DockTrailerStatusEnum val)
    {
        return GetCode(val);
    }
    
    public static string ToCode(this EncodingType val)
    {
        return GetCode(val);
    }
    
    private static string GetCode(object val)
    {
        var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
    
        return attributes.Length > 0 ? attributes[0].Description : string.Empty;
    }