在另一个应用程序域中加载独立程序集

发布于 2024-09-07 21:59:45 字数 1209 浏览 2 评论 0原文

简单的问题,也许你很容易回答。

我的应用程序的同一输出文件夹中有一个名为“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);

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

文件未找到异常

这是为什么呢?

谢谢你的时间。

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);

Everytime the line indicated as not working throws a

FileNotFoundException

.

Why's that ?

Thanks for you time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-09-14 21:59:46

将“C:\Output\Debug\OptimeToolbench\”添加到 AppDomain 的 PrivateBinPath 中。另外,不要传递文件名,而是传递程序集名称——我假设是 MigrationSteps。

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.

回眸一遍 2024-09-14 21:59:46

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文