开发者

Load independent assembly in a another App Domain

开发者 https://www.devze.com 2023-01-05 12:33 出处:网络
simple question, probably easy for you to answer. I have a dll named \"MigrationSteps.dll\" in the same output folder of my application.

simple question, probably easy for you to answer.

I have a dll named "MigrationSteps.dll" in the same output folder of my application. What I want to do, is to load this assembly in a new AppDomain and execute a method on an instance of a class inside this DLL.

Here's my code

       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开发者_JS百科);

Everytime the line indicated as not working throws a

FileNotFoundException

.

Why's that ?

Thanks for you time.


Add "C:\Output\Debug\OptimeToolbench\" to the PrivateBinPath of the AppDomain. Also do not pass in the file name, pass in the assembly name -- I'm assuming that would be MigrationSteps.


appDomain.Load(string) takes in an assembly name (the strong-name) - NOT the path of where the file is on disk!

0

精彩评论

暂无评论...
验证码 换一张
取 消