在另一个应用程序域中加载独立程序集
简单的问题,也许你很容易回答。
我的应用程序的同一输出文件夹中有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将“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.
appDomain.Load(string) 接受程序集名称(强名称) - 不是文件在磁盘上的路径!
appDomain.Load(string) takes in an assembly name (the strong-name) - NOT the path of where the file is on disk!