About Assembly [C #]
LScenario
You are a developer of adventure works. Currently, you need to create an application to report the Code Compiled by the company's developers. The application loads an assembly and uses reflection to generate the report to be used for code review and documentation. To improve performance, you need to create the application as a console application. In this exercise, the Assembly class is used to load the assembly, and the Assembly name and path should be provided by the user.
You want to create an application that documents the code written in different applications. In this exercise, the application uses multiple information type reflection classes to generate a console-based report for code review and documentation. This report lists the Assembly name, location, version, and other Assembly-level metadata. The report also lists the modules, classes, members, methods, and attributes in the Assembly.
Step 1: Display assembly information
1) declare a method named generatereport that accepts the Assembly as a parameter.
2) write code to display assembly information.
Displays the Assembly name and position and assembly status based on the Assembly location in the Global Assembly Cache.
Result: The Assembly information is displayed.
Step 2: display all modules and types in the Assembly
1) Use the getmodules method to create a report that contains modules in the Assembly.
This Code calls the displaymoduleinfo method for each module in the Assembly.
2) declare a method named displaymoduleinfo that accepts the module method as the parameter.
3) use the gettypes method to display the list of types contained in each method.
This Code calls the displaytypeinfo method for each type in the module.
Result: The module list and the type list of each module are displayed.
Step 3: display fields, attributes, and methods of all types.
1) declare a method that accepts type as the parameter named displaytypeinfo.
2) use the getfields method to display the list of fields of all types.
The list should contain the following information:
L field name
L field type
L whether the field is public or private
3) use the getproperties method to display the list of all properties.
The list should contain the following information:
L attribute name
L attribute type
L whether the attribute is readable or writable
4) displays a list of all methods of the current type.
This Code calls the displaymethodinfo method for each method of the type.
Result: The field list, attribute list, and method list of each type are displayed.
Step 4: display information contained in all methods
1) declare a method that accepts methodinfo as the parameter named displaymethodinfo.
2) use the getparameters method to display the method header file information.
3) use the getparameters method to display the list of all parameters.
The list should contain the following information:
L parameter name
L parameter type
Whether the parameter is isoptional, isout, or isretval.
4) use the getmethodbody method to display information about each method.
This information should contain the stack size and local variable details defined in the method, such as localindex and localtype.
5) Compile and run the application.
The main code is as follows:
1 using system;
2 using system. Collections. Generic;
3 using system. text;
4 using system. reflection;
5
6 namespace crse31_ AE _lab01
7 {
8 class Program
9 {
10 static string readassemblyfullpath ()
11 {
12 string assemblypath, assemblyname;
13 console. Write ("type the assembly path :");
14 assemblypath = console. Readline ();
15 console. Write ("type the Assembly name :");
16 assemblyname = console. Readline ();
17 return assemblypath + @ "/" + assemblyname;
18}
19
20 static void main (string [] ARGs)
21 {
22 string assemblyfullpath;
23 assemblyfullpath = readassemblyfullpath ();
24 generatereport (assemblyfullpath );
25 console. Read ();
26}
27 static void generatereport (string path)
28 {
29 assembly myassemble = assembly. LoadFile (PATH );
30 console. writeline (myassemble. fullname );
31 // myassemble.
32 console. writeline (string. Empty. padleft (45 ,'-'));
33 foreach (type in myassemble. gettypes ())
34 {
35 console. writeline ("the name of assemble is:" + type. Name );
36 console. writeline ("the path is:" + type. fullname );
37}
38 getmodules (myassemble );
39}
40 static void getmodules (Assembly name)
41 {
42 console. writeline (string. Empty. padleft (40 ,'-'));
43 foreach (module in name. getmodules ())
44 {
45 console. writeline ("module name:" + module. Name );
46 displaymoduleinfo (module );
47}
48}
49 static void displaymoduleinfo (module)
50 {
51 console. writeline (string. Empty. padleft (35 ,'-'));
52 foreach (type in module. gettypes ())
53 {
54 console. writeline ("module type name is:" + type. Name );
55 displaytypeinfo (type );
56}
57}
58 static void displaytypeinfo (type)
59 {
60 foreach (fieldinfo in type. getfields ())
61 {
62 console. writeline ("the name of this field is:" + fieldinfo. Name );
63 console. writeline ("the type of this field is:" + fieldinfo. GetType (). tostring ());
64 if (fieldinfo. isprivate)
65 {
66 console. writeline ("this field is private! ");
67}
68 else
69 {
70 console. writeline ("this field is public ");
71}
72}
73 console. writeline (string. Empty. padleft (30 ,'-'));
74 foreach (propertyinfo in type. getproperties ())
75 {
76 console. writeline ("the name of this property is:" + propertyinfo. Name );
77 console. writeline ("type of this property is:" + propertyinfo. propertytype. tostring ());
78 If (propertyinfo. canwrite)
79 {
80 console. writeline ("this property can write! ");
81}
82 else
83 {
84 console. writeline ("this property can't be write ");
85}
86}
87 console. writeline (string. Empty. padleft (30 ,'-'));
88 foreach (methodinfo method in type. getmethods ())
89 {
90 console. writeline ("method name:" + method. Name );
91 displaymethodinfo (method );
92}
93}
94 static void displaymethodinfo (methodinfo method)
95 {
96 console. writeline (string. Empty. padleft (25 ,'-'));
97 foreach (parameterinfo parinfo in method. getparameters ())
98 {
99 console. writeline ("parameter name is:" + parinfo. Name );
100 console. writeline ("parameter type is:" + parinfo. parametertype. tostring ());
101 if (parinfo. isoptional)
102 {
103 console. writeline ("this parameter is option! ");
104}
105 else
106 console. writeline ("This parrameter is not option! ");
107 If (parinfo. isout)
108 {
109 console. writeline ("this parameter is out! ");
110}
111 else
112 console. writeline ("this parameter is not out! ");
113 If (parinfo. isretval)
114 console. writeline ("and is retval ");
115 else
116 console. writeline ("and is not retval ");
117}
118 If (method. getmethodbody ()! = NULL)
119 {
120 getmethodbody (method. getmethodbody ());
121}
122}
123 static void getmethodbody (methodbody body)
124 {
125 console. writeline (string. Empty. padleft (20 ,'-'));
126 console. writeline ("body information :");
127 console. writeline ("Max stack size:" + body. maxstacksize );
128 console. writeline ("local variables are initialized:" + body. initlocals );
129 console. writeline (string. Empty. padleft (15 ,'-'));
130 foreach (localvariableinfo local in body. localvariables)
131 {
132 console. writeline ("index of local var is:" + local. localindex );
133 console. writeline ("type of local var is:" + local. localtype. tostring ());
134}
135 console. writeline (string. Empty. padleft (20 ,'*'));
136}
137}
138}