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"));
时间: 2024-11-05 16:06:08