在另一个App Domain中加载独立程序集

时间:2010-06-28 21:45:08

标签: c# .net assemblies appdomain

简单的问题,可能很容易回答。

我的应用程序的同一个输出文件夹中有一个名为“MigrationSteps.dll”的dll。 我想要做的是将此程序集加载到新的AppDomain中,并在此DLL内的类的实例上执行方法。

这是我的代码

       string migrationStepsDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MigrationSteps.dll");
        AppDomainSetup appDomainSetup = new AppDomainSetup() { PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory };
        Evidence evidence = AppDomain.CurrentDomain.Evidence;

        AppDomain appDomain = AppDomain.CreateDomain("MigrationAppDomain", evidence, appDomainSetup);

 //NOT WORKING
        Assembly assembly = appDomain.Load(@"C:\Output\Debug\OptimeToolbench\MigrationSteps.dll");

        //WORKING
        Assembly assembly = Assembly.LoadFrom(@"C:\Output\Debug\OptimeToolbench\MigrationSteps.dll"); ****works.

        //This part works well
        Type type = assembly.GetType("MigrationSteps.Foo");
        object foo = Activator.CreateInstance(type);
        MethodInfo methodInfo = type.GetMethod("HelloWorld");
        methodInfo.Invoke(foo, null);
        AppDomain.Unload(appDomain);

每次表示不工作的行都会抛出

  

FileNotFoundException异常

为什么?

谢谢你的时间。

2 个答案:

答案 0 :(得分:3)

appDomain.Load(string)接收程序集名称(强名称) - 而不是文件在磁盘上的路径!

答案 1 :(得分:3)

将“C:\ Output \ Debug \ OptimeToolbench”添加到AppDomain的PrivateBinPath。也不要传入文件名,传入程序集名称 - 我假设这将是MigrationSteps。