public class testDescrible { int _tt; [Description("123")] public int ttt { get { return _tt; } set { _tt = value; } } [Description("测试1")] public void TTT() { } [Description("测试2")] public void TTT(int A) { } }
testDescrible tst = new testDescrible(); Type t = tst.GetType(); var attributesww = TypeDescriptor.GetProperties(t); //方法遍历,遍历实体类的相关public方法,不包含继承的相关方法 foreach (System.Reflection.MemberInfo p in t.GetMethods(System.Reflection.BindingFlags.Instance| System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.DeclaredOnly)) { object[] Attribute1 = p.GetCustomAttributes(typeof(DescriptionAttribute), true);//只获取DescriptionAttribute的特性 object[] Attribute1123 = p.GetCustomAttributes(false); //if (Attribute1.Length > 0) //{ // DescriptionAttribute myAttribute = (DescriptionAttribute)Attribute1[0]; // string aaa = myAttribute.Description; //} //object[] Attribute2 = p.GetCustomAttributes(typeof(QuickWebApiAttribute), false); } //属性遍历 foreach (System.Reflection.PropertyInfo pi in t.GetProperties()) { AttributeCollection attributes = TypeDescriptor.GetProperties(t)[pi.Name].Attributes; DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)]; string describle = myAttribute.Description; }