When I use code for dynamic generation, the code to be compiled is:
1 using system;
2 using system. Windows. forms;
3 using system. Threading;
4 using system. reflection;
5 using system. IO;
6
7 using system. Security;
8 using system. Security. permissions;
9
10
11/*** // <summary>
12 /// Summary of class1.
13 /// </Summary>
14 ///
15
16 public class test: financialbyrefobject
17 {
18 public void testmethod (string srcappdomain)
19 {
20 FileStream fs = File. Create ("11.txt ");
21 fs. Close ();
22
23}
24 public void Click (object sender, System. EventArgs e)
25 {
26 MessageBox. Show ("hi ");
27}
28}
The compilation attributes used are:
1 compilerparams. generateexecutable = false;
2 compilerparams. generateinmemory = false;
3 compilerparams. includedebuginformation = false;
In this way, compiler will be in the system temp (that is, c: \ Documents ents and Settings \ *** \ Local Settings \ Temp) directory when you execute the code. but when you create an application domain. 1 appdomain domain = appdomain. createdomain ("mynewdomain", null, null );
Then, when the generated assembly is loaded using this domain: 1 object test = domain. createinstancefromandunwrap (assembly. codebase, "test ");
Note: assembly is obtained after compilation.
However, the following code finds that: 1 methodinfo [] MIS = test. GetType (). getmethods ();
2 foreach (MethodInfo min in mis)
3 {
4 MessageBox. Show (min. Name );
5}
The output is not the result I want. Only the method of externalbyrefobject has no Click
TestMethod.
However, if I use the following code to load: 1 object test = Activator. CreateInstanceFrom (assembly. CodeBase, "Test"). Unwrap ();
You can get the correct result. This is a problem. We need to study it.