首页 > 代码库 > c#获取Attribute设置的值

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

 

c#获取Attribute设置的值