阅读自定义属性

阅读自定义属性

问题描述:

System.Reflection.PropertyInfo[] props = myInp.GetProperties();
foreach (var prop in props)
{
  if (prop.Name == "XX")
  {
    var customAttribute = prop.GetCustomAttribute(typeof(CustomAttribute)) as CustomAttribute; //This is always null for XX                      
     }
}


    public class MyClass
    {

    [CustomAttribute(MyProp1 = true, MyProp2="test")]
    public bool X{ get ; set;}, 

    [CustomAttribute(MyProp1 = true)]   
    public MyEnum XX{ get ; set;}

    }


    public enum MyEnum
        {
            ABC = 0,
            XYZ = 1
        }


    public class CustomAttribute : Attribute
    {
        public bool MyProp1 { get; set; }
            public string MyProp2 { get; set; }

    }   

你好,

 有问题吗?这个论坛不是一个pastebin。谢谢:)

 Is there a question? This forum is not a pastebin. Thanks :)