@@ -16,28 +16,83 @@ static void Main(string[] args)
16
16
Type myType = typeof ( MyClass1 ) ;
17
17
18
18
// Display the attributes of MyClass1.
19
- Object [ ] myAttributes = myType . GetCustomAttributes ( true ) ;
19
+ object [ ] myAttributes = myType . GetCustomAttributes ( true ) ;
20
20
if ( myAttributes . Length > 0 )
21
21
{
22
22
Console . WriteLine ( $ "\n The attributes for the class '{ myType . Name } ' are:") ;
23
- for ( int j = 0 ; j < myAttributes . Length ; j ++ )
23
+ for ( int j = myAttributes . Length - 1 ; j >= 0 ; j -- )
24
+ {
24
25
Console . WriteLine ( $ " { myAttributes [ j ] } ") ;
26
+ }
25
27
}
26
28
27
29
// Get the methods associated with MyClass1.
28
30
MemberInfo [ ] myMethods = myType . GetMethods ( ) ;
29
31
32
+ Console . WriteLine ( "" ) ;
33
+ Console . WriteLine ( $ "'{ myType . Name } ' type has '{ myMethods . Length } ' methods") ;
34
+
30
35
// Display the attributes for each of the methods of MyClass1.
31
36
for ( int i = 0 ; i < myMethods . Length ; i ++ )
32
37
{
38
+ Console . WriteLine ( "" ) ;
39
+ Console . WriteLine ( $ "Getting custom attributes for '{ myMethods [ i ] . Name } '") ;
40
+
33
41
myAttributes = myMethods [ i ] . GetCustomAttributes ( true ) ;
42
+
43
+ Console . WriteLine ( "" ) ;
44
+ Console . WriteLine ( $ "'{ myMethods [ i ] . Name } ' method has { myAttributes . Length } custom attributes") ;
45
+
34
46
if ( myAttributes . Length > 0 )
35
47
{
36
48
Console . WriteLine ( $ "\n The attributes for the method '{ myMethods [ i ] . Name } ' of class '{ myType . Name } ' are:") ;
49
+
37
50
for ( int j = 0 ; j < myAttributes . Length ; j ++ )
51
+ {
38
52
Console . WriteLine ( $ " { myAttributes [ j ] } ") ;
39
-
40
- var attributeData = myMethods [ i ] . GetCustomAttributesData ( ) ;
53
+ }
54
+
55
+ foreach ( var attrib in myAttributes )
56
+ {
57
+ // check if the method has Attribute1
58
+ if ( attrib is Attribute1Attribute )
59
+ {
60
+ Console . WriteLine ( $ " >>>>>>> { myMethods [ i ] . Name } has 'Attribute1' attribute") ;
61
+ }
62
+
63
+ // check if the method has IgnoreAttribute
64
+ if ( attrib is IgnoreAttribute )
65
+ {
66
+ Console . WriteLine ( $ " >>>>>>> { myMethods [ i ] . Name } has 'IgnoreAttribute' attribute") ;
67
+ }
68
+
69
+ // check if the method has DataRowAttribute
70
+ if ( attrib is DataRowAttribute )
71
+ {
72
+ Console . WriteLine ( $ " >>>>>>> { myMethods [ i ] . Name } has 'DataRowAttribute' attribute") ;
73
+
74
+ DataRowAttribute attDataRow = ( DataRowAttribute ) attrib ;
75
+
76
+ int index = 0 ;
77
+
78
+ foreach ( var dataRow in attDataRow . Arguments )
79
+ {
80
+ Console . WriteLine ( $ " DataRowAttribute.Arg[{ index ++ } ] has: { dataRow } ") ;
81
+ }
82
+ }
83
+
84
+ // check if the method has ComplexAttribute
85
+ if ( attrib is ComplexAttribute )
86
+ {
87
+ Console . WriteLine ( $ " >>>>>>> { myMethods [ i ] . Name } has 'ComplexAttribute' attribute") ;
88
+
89
+ ComplexAttribute attDataRow = ( ComplexAttribute ) attrib ;
90
+
91
+ Console . WriteLine ( $ " ComplexAttribute.Max is { attDataRow . Max } ") ;
92
+ Console . WriteLine ( $ " ComplexAttribute.B is { attDataRow . B } ") ;
93
+ Console . WriteLine ( $ " ComplexAttribute.S is { attDataRow . S } ") ;
94
+ }
95
+ }
41
96
}
42
97
}
43
98
@@ -52,8 +107,11 @@ static void Main(string[] args)
52
107
if ( myAttributes . Length > 0 )
53
108
{
54
109
Console . WriteLine ( $ "\n The attributes for field '{ myFields [ i ] . Name } ' of class '{ myType . Name } ' are:") ;
110
+
55
111
for ( int j = 0 ; j < myFields . Length ; j ++ )
112
+ {
56
113
Console . WriteLine ( $ " { myFields [ j ] } ") ;
114
+ }
57
115
58
116
var attributeData = myFields [ i ] . GetCustomAttributesData ( ) ;
59
117
}
0 commit comments