Use reflection technology to list type information!
Last Update:2018-12-07
Source: Internet
Author: User
1 Using System;
2 Using System. reflection;
3
4 Namespace Leleapplication2 {
5 Class Class1 {
6 [Stathread]
7 Static Void Main ( String [] ARGs ){
8 System. type T = Typeof (System. String );
9 // Get Constructor
10 Console. writeline ( " Get the constructor list " );
11 System. reflection. constructorinfo [] Ci = T. getconstructors (system. reflection. bindingflags. Public | System. reflection. bindingflags. instance );
12 Foreach (System. reflection. memberinfo Mi In CI ){
13 Console. writeline (MI. tostring ());
14 }
15 Console. writeline ( " --------------------------------------------------------- " );
16 Console. writeline ( " Number of Members obtained " );
17 // Obtain the number of members of the type.
18 Memberinfo [] MII = T. getmembers ();
19 Console. writeline ( " {0} type {1} members " , T. fullname, MII. Length );
20 If (T. ispublic ){
21 Console. writeline ( " The visibility of {0} is public. " , T. fullname );
22 }
23 Console. writeline ( " --------------------------------------------------------- " );
24 // Lists all fields, attributes, methods, and events of a type.
25 Console. writeline ( " Obtain the Field List " );
26 Fieldinfo [] Fi = T. getfields (bindingflags. Static | Bindingflags. nonpublic | Bindingflags. Public | Bindingflags. instance );
27 Foreach (Memberinfo Mi In FI ){
28 Console. writeline (MI. tostring ());
29 }
30 Console. writeline ( " --------------------------------------------------------- " );
31 Console. writeline ( " Get attribute list " );
32 Propertyinfo [] pi = T. getproperties (bindingflags. Static | Bindingflags. nonpublic | Bindingflags. Public | Bindingflags. instance );
33 Foreach (Memberinfo Mi In Pi ){
34 Console. writeline (MI. tostring ());
35 }
36 Console. writeline ( " --------------------------------------------------------- " );
37 Console. writeline ( " Retrieve Method list " );
38 Methodinfo [] MMI = T. getmethods (bindingflags. Static | Bindingflags. nonpublic | Bindingflags. Public | Bindingflags. instance );
39 Foreach (Memberinfo Mi In MMI ){
40 Console. writeline (MI. tostring ());
41 }
42 Console. writeline ( " --------------------------------------------------------- " );
43 Console. writeline ( " Get event list " );
44 Eventinfo [] EI = T. getevents (bindingflags. Static | Bindingflags. nonpublic | Bindingflags. Public | Bindingflags. instance );
45 Foreach (Memberinfo Mi In EI ){
46 Console. writeline (MI. tostring ());
47 }
48 }
49 }
50 }
51