首页 > 其他 > 详细

c#获取Attribute设置的值

时间:2014-09-11 18:59:22      阅读:335      评论:0      收藏:0      [点我收藏+]

1,

    public class Model
    {
        [Test(Name="zhu")]
        public string Id { get; set; }
    }

2,

    public class TestAttribute : Attribute
    {
        public string Name;

        public static string GetAttrClass(Model m,string name)
        {
            Type type = m.GetType();
            PropertyInfo f = type.GetProperty(name);
            if (f == null)
            {
                return null;
            }

            TestAttribute t = Attribute.GetCustomAttribute(f, typeof (TestAttribute)) as TestAttribute;

            return t.Name;
        }

    }

3,

            Model m = new Model();
            Console.WriteLine(TestAttribute.GetAttrClass(m,"Id"));

 

c#获取Attribute设置的值

原文:http://www.cnblogs.com/zhushangwei/p/3966928.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!