C#反射读取类的类注释,⽅法注释,属性注释
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace AttributeTest
8{
9 [AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
10 class AttributeInfo:Attribute
11 {
12 private string name;
13 private string type;
14 private int age;
15 public AttributeInfo(string name,string type,int age)
16 {
17 this.name = name;
18 pe = type;
19 this.age = age;
20 }
21 public string Name
22 {
23 get
24 {
25 return name;
26 }
27 }
28 public string Type
29 {
30 get
31 {
32 return type;
33 }
34 }
35 public int Age
36 {
37 get
38 {
39 return age;
40 }
41 }
42
43 public string message { get; set; }
44 }
45}
1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace AttributeTest
10{
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 Console.OutputEncoding = Encoding.Unicode;
15 Console.OutputEncoding = Encoding.Unicode;
16
17 Type info = typeof(testAttr);
18
19 PropertyInfo[] ps = info.GetProperties();
20
21 Console.WriteLine("遍历⾃定义类的属性");
22 object[] attributes = info.GetCustomAttributes(false);
23 for (int i = 0; i < attributes.Length; i++)
24 {
25 AttributeInfo t = (AttributeInfo)attributes[i];
26 System.Console.WriteLine(t.Name+" "+t.Type+" "+t.message);
27 }
28 Console.WriteLine("遍历⽅法的⾃定义属性");
29 var methods=info.GetMethods();
30 foreach(var method in methods)
31 {
32 var mattrs = method.GetCustomAttributes(false);
33 for (int i = 0; i < mattrs.Length; i++)
34 {
35 AttributeInfo t = mattrs[i] as AttributeInfo;
36 if(t!=null)
37 {
38 Console.WriteLine("⽅法名:" + method.Name);
39 System.Console.WriteLine(t.Name + " " + t.Type + " " + t.message);
40 }
41
42 }
43 }
44 Console.WriteLine("遍历属性的⾃定义属性");
45 var pss=info.GetProperties();
46 foreach(var p in pss)
47 {
48 var pattrs=p.GetCustomAttributes(false);
49 for (int i = 0; i < pattrs.Length; i++)
50 {
51 AttributeInfo t = pattrs[i] as AttributeInfo;
52 if(t!=null)
53 {
54 Console.WriteLine("属性名:" + p.Name);
55 System.Console.WriteLine(t.Name + " " + t.Type + " " + t.message);
56 }
57
58 }
59 }
60
61
62
63
64
65 Console.ReadKey();
66 }
67 }
68 [AttributeInfo("张三","男",29,message ="测试⼈员信息")]
69 [AttributeInfo("李四","⼥",229,message ="测试⼈员信息222")]
70 class testAttr
71 {
72 [AttributeInfo("王五","不男不⼥",33,message ="泰国⼈妖")]
73 public int sum(int a,int b)
74 {
75 return a + b;
76 }
77 [AttributeInfo("乘法", "100", 200, message = "乘法⼝诀")]
78 public int cheng(int a, int b)
79 {
80 return a + b;
80 return a + b;
81 }
82 [AttributeInfo("属性1", "值1", 1)]
83 public string lotnum { get; set; }
84 [AttributeInfo("属性2", "值2", 2)]
85 public string partnum { get; set; }
86 }
87}
最后附带 相关知识点详细定义
1预定义特性(Attribute)
2.Net 框架提供了三种预定义特性:
3• AttributeUsage
4• Conditional
5• Obsolete
6
7
8AttributeUsage
9预定义特性 AttributeUsage 描述了如何使⽤⼀个⾃定义特性类。它规定了特性可应⽤到的项⽬的类型。
10规定该特性的语法如下:
11[AttributeUsage(
12 validon,
13 AllowMultiple=allowmultiple,
14 Inherited=inherited
15)]
16其中:
17• 参数 validon 规定特性可被放置的语⾔元素。它是枚举器 AttributeTargets 的值的组合。默认值是 AttributeTargets.All。
18• 参数 allowmultiple(可选的)为该特性的 AllowMultiple 属性(property)提供⼀个布尔值。如果为 true,则该特性是多⽤的。默认值是 false(单⽤的)。19⽐如[AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
20 class AttributeInfo:Attribute
21那么[AttributeInfo("张三","男",29,message ="测试⼈员信息")]
22 [AttributeInfo("张三2","男",229,message ="测试⼈员信息")]
23 class testAttr
24就不会报错了
24就不会报错了
25• 参数 inherited(可选的)为该特性的 Inherited 属性(property)提供⼀个布尔值。如果为 true,则该特性可被派⽣类继承。默认值是 false(不被继承)。26
27
28
29
30
31[AttributeUsage(AttributeTargets.Class |
32AttributeTargets.Constructor |
33AttributeTargets.Field |
34AttributeTargets.Method |
35AttributeTargets.Property,
36AllowMultiple = true)]
37
38
39Conditional
40这个预定义特性标记了⼀个条件⽅法,其执⾏依赖于指定的预处理标识符。
41它会引起⽅法调⽤的条件编译,取决于指定的值,⽐如 Debug 或 Trace。
42
43
44static void Main(string[] args)
45 {
46 DebugTest.alert("inmain"); 只有VS在Debug的时候才会调⽤这个⽅法
47 DebugTest.alert2("inmain2");
48 Console.ReadKey();
49 }
50
51
52[Conditional("DEBUG")]
53 public static void alert(string msg)
54 {
55 Console.WriteLine(msg);
56 }
57Obsolete
58[Obsolete(
59 message, 提⽰类⽅法过去的⽂字提⽰内容
60 iserror false,⽅法还能⽤.ture 那么编译器就会报错
61)]
62[Obsolete("⽅法已过时",false)]//如果是true,那么编译器会报错
63 public static void alert2(string msg)
64 {
65 Console.WriteLine(msg);
writeline方法属于类66 }
67
68
69
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论