c#获取Attribute设置的值

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"));

 

转载于:https://www.cnblogs.com/zhushangwei/p/3966928.html

你可能感兴趣的:(c#获取Attribute设置的值)