0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoCallBack
{
class Program
{
static void Main(string[] args)
{
AppDomain newDomain = AppDomain.CreateDomain("New Domain");
Console.WriteLine(newDomain.BaseDirectory);
newDomain.DoCallBack(new CrossAppDomainDelegate(SayHello));
AppDomain.Unload(newDomain);
}
}
}
我想在新的应用程序域中调用SayHello()方法。让我们假设,HelloMethod DLL是第三方,我没有代码。我只有组装。但我知道它有SayHello()方法。我能做什么?在新的应用程序域中启动第三方DLL中的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloMethod
{
class Program
{
static void Main(string[] args)
{
}
static void SayHello()
{
Console.WriteLine("Hi from " + AppDomain.CurrentDomain.FriendlyName);
}
}
}
在当前的代码,它给错误
感谢分配,**新的CrossAppDomainDelegate **是必要的吗?为什么我们可以没有这个运行? – SHRI 2013-02-25 10:30:23
@SHRI有必要!编译器为您声明的每个委托类型生成代码。 CrossAppDomainDelegate可能让编译器知道委托应该从MarshalByRefObject派生,以允许跨AppDomain通信。这是我的猜测。重要的是你需要使用CrossAppDomainDelegate。 – 2013-02-25 13:00:58