程序域(一個程序中調(diào)用另一個程序)
一個exe中可以調(diào)用另一個exe,或另一個exe的方法:
-------------用程序域?qū)崿F(xiàn):
AppDomain 類用于創(chuàng)建和中斷應(yīng)用程序域。
1建立控制臺AssemblyA
using System;
namespace Wrox.ProCSharp.Assemblies.AppDomains

{
class Class1
{
public Class1(int val1, int val2)
{
Console.WriteLine("Constructor with the values {0}, {1}" +
" in domain {2} called", val1, val2,
AppDomain.CurrentDomain.FriendlyName);
}
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Main in domain {0} called",
AppDomain.CurrentDomain.FriendlyName);
}
}
}
2建立控制臺DomainTest
using System;
namespace Wrox.ProCSharp.Assemblies.AppDomains

{
class Test
{
[STAThread]
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
Console.WriteLine(currentDomain.FriendlyName);
AppDomain secondDomain =
AppDomain.CreateDomain("New AppDomain");
// secondDomain.ExecuteAssembly("AssemblyA.exe");
secondDomain.CreateInstance("AssemblyA",
"Wrox.ProCSharp.Assemblies.AppDomains.Class1", true,
System.Reflection.BindingFlags.CreateInstance,
null, new object[]
{7, 3}, null, null, null);
}
}
}
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
---------------------用程序集的反射也可以實現(xiàn)一個exe調(diào)用另一個exe的方法!
:system.Type 可以訪問任何給定的數(shù)據(jù)類型的信息
:system.Assembly訪問給定程序集的元數(shù)據(jù),也可以加載和執(zhí)行程序集的方法
posted on 2006-01-05 16:38 夢在天涯 閱讀(1368) 評論(1) 編輯 收藏 引用 所屬分類: C#/.NET

