发现assembly的dll在temp里,AppDomain无法动态加载那个assembly
我用代码动态生成时,需要编译的代码是:
1using System;
2using System.Windows.Forms;
3using System.Threading;
4using System.Reflection;
5using System.IO;
6
7using System.Security;
8using System.Security.Permissions;
9
10
11 /// <summary>
12 /// Class1 的摘要说明。
13 /// </summary>
14 ///
15
16 public class Test : MarshalByRefObject
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 }
2using System.Windows.Forms;
3using System.Threading;
4using System.Reflection;
5using System.IO;
6
7using System.Security;
8using System.Security.Permissions;
9
10
11 /// <summary>
12 /// Class1 的摘要说明。
13 /// </summary>
14 ///
15
16 public class Test : MarshalByRefObject
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 }
采用的编译属性是:
1 compilerParams.GenerateExecutable=false;
2 compilerParams.GenerateInMemory=false;
3 compilerParams.IncludeDebugInformation=false;
这样,你执行代码时,Compiler就会在系统temp(就是:C:\Documents and Settings\****\Local Settings\Temp)目录下.但是当你创建一个应用程序域。2 compilerParams.GenerateInMemory=false;
3 compilerParams.IncludeDebugInformation=false;
1AppDomain domain = AppDomain.CreateDomain("MyNewDomain", null, null);
然后通过这个域去加载刚才生成的assembly时:1object test = domain.CreateInstanceFromAndUnwrap(assembly.CodeBase, "Test");
注:assembly就是刚才编译后得到的。但是通过下列代码发现:
1MethodInfo [] mis = test.GetType().GetMethods();
2 foreach(MethodInfo min in mis)
3 {
4 MessageBox.Show(min.Name);
5 }
出来的并不是我想要的结果,只有MarshalByRefObject的方法,并没有Click 2 foreach(MethodInfo min in mis)
3 {
4 MessageBox.Show(min.Name);
5 }
TestMethod 的方法。
但是我若用下列代码加载:
1object test = Activator.CreateInstanceFrom(assembly.CodeBase,"Test").Unwrap();
就能得到正确的结果。是个问题,得研究研究