程序域
2006-01-16 13:04 Timothy Ye 阅读(273) 评论(0) 编辑 收藏 举报一个exe中可以调用另一个exe,或另一个exe的方法:
-------------用程序域实现:
AppDomain 类用于创建和中断应用程序域。
1建立控制台AssemblyA
1using System;
2
3namespace Wrox.ProCSharp.Assemblies.AppDomains
4{
5 class Class1
6 {
7 public Class1(int val1, int val2)
8 {
9 Console.WriteLine("Constructor with the values {0}, {1}" +
10 " in domain {2} called", val1, val2,
11 AppDomain.CurrentDomain.FriendlyName);
12 }
13 [STAThread]
14 static void Main(string[] args)
15 {
16 Console.WriteLine("Main in domain {0} called",
17 AppDomain.CurrentDomain.FriendlyName);
18 }
19 }
20}
21
产生。exe。2
3namespace Wrox.ProCSharp.Assemblies.AppDomains
4{
5 class Class1
6 {
7 public Class1(int val1, int val2)
8 {
9 Console.WriteLine("Constructor with the values {0}, {1}" +
10 " in domain {2} called", val1, val2,
11 AppDomain.CurrentDomain.FriendlyName);
12 }
13 [STAThread]
14 static void Main(string[] args)
15 {
16 Console.WriteLine("Main in domain {0} called",
17 AppDomain.CurrentDomain.FriendlyName);
18 }
19 }
20}
21
2建立控制台DomainTest
1using System;
2namespace Wrox.ProCSharp.Assemblies.AppDomains
3{
4 class Test
5 {
6 [STAThread]
7 static void Main(string[] args)
8 {
9 AppDomain currentDomain = AppDomain.CurrentDomain;
10 Console.WriteLine(currentDomain.FriendlyName);
11 AppDomain secondDomain =
12 AppDomain.CreateDomain("New AppDomain");
13 // secondDomain.ExecuteAssembly("AssemblyA.exe");
14 secondDomain.CreateInstance("AssemblyA",
15 "Wrox.ProCSharp.Assemblies.AppDomains.Class1", true,
16 System.Reflection.BindingFlags.CreateInstance,
17 null, new object[] {7, 3}, null, null, null);
18
19 }
20 }
21}
22
结果:2namespace Wrox.ProCSharp.Assemblies.AppDomains
3{
4 class Test
5 {
6 [STAThread]
7 static void Main(string[] args)
8 {
9 AppDomain currentDomain = AppDomain.CurrentDomain;
10 Console.WriteLine(currentDomain.FriendlyName);
11 AppDomain secondDomain =
12 AppDomain.CreateDomain("New AppDomain");
13 // secondDomain.ExecuteAssembly("AssemblyA.exe");
14 secondDomain.CreateInstance("AssemblyA",
15 "Wrox.ProCSharp.Assemblies.AppDomains.Class1", true,
16 System.Reflection.BindingFlags.CreateInstance,
17 null, new object[] {7, 3}, null, null, null);
18
19 }
20 }
21}
22
DomainTest.exe
Constructor with the values 7, 3 in domain New AppDomain called
Press any key to continue
或
DomainTest.exe
Constructor with the values 7, 3 in domain New AppDomain called
Press any key to continue
---------------------
用程序集的反射也可以实现一个exe调用另一个exe的方法!
:system.Type 可以访问任何给定的数据类型的信息
:system.Assembly访问给定程序集的元数据,也可以加载和执行程序集的方法