阅读自定义属性 [英] Reading custom attributes

查看:90
本文介绍了阅读自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 :)


这篇关于阅读自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆