C# Reflection.Net:如何加载依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/181901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reflection.Net: how to load dependencies?
提问by Olivier de Rivoyre
I try to add an addons system to my Windows.Net application using Reflection; but it fails when there is addon with dependencie.
Addon class have to implement an interface 'IAddon' and to have an empty constructor.
Main program load the addon using Reflection:
我尝试使用反射将插件系统添加到我的 Windows.Net 应用程序;但是当有依赖项的插件时它会失败。
Addon 类必须实现一个接口 'IAddon' 并有一个空的构造函数。
主程序使用反射加载插件:
Assembly assembly = Assembly.LoadFile(@"C:\Temp\TestAddon\Addon.dll");
Type t = assembly.GetType("Test.MyAddon");
ConstructorInfo ctor = t.GetConstructor(new Type[] { });
IAddon addon= (IAddon) ctor.Invoke(new object[] { });
addon.StartAddon();
It works great when addon do not use dependencie.
But if my addon reference and use an another DLL (C:\Temp\TestAddon\MyTools.dll) that is saved near the addon in disk, it fails:
System.IO.FileNotFoundException: Could not load file or assembly 'MyTools.dll' or one of its dependencies.
当插件不使用依赖时,它工作得很好。但是,如果我的插件引用并使用另一个 DLL (C:\Temp\TestAddon\MyTools.dll) 保存在磁盘中的插件附近,它就会失败:
System.IO.FileNotFoundException: 无法加载文件或程序集“MyTools.dll” ' 或其依赖项之一。
I do not wants to copy the addons DLL near my executable, how can i do to tell .Net runtime to search in "C:\Temp\TestAddon\" for any dependency?
我不想复制我的可执行文件附近的插件 DLL,我该如何告诉 .Net 运行时在“C:\Temp\TestAddon\”中搜索任何依赖项?
Note that adding
注意添加
Assembly assembly = Assembly.LoadFile(@"C:\Temp\TestAddon\MyTools.dll");
do not change anything.
不要改变任何东西。
采纳答案by Andreas Tschager
If MyTools.dllis located in the same directory as Addon.dll, all you need to do is call Assembly.LoadFrom
instead of Assembly.LoadFile
to make your code work. Otherwise, handling the AppDomain.AssemblyResolve
event is the way to go.
如果MyTools.dll位于同一目录Addon.dll,所有你需要做的就是打电话Assembly.LoadFrom
,而不是Assembly.LoadFile
让你的代码工作。否则,处理AppDomain.AssemblyResolve
事件是要走的路。
回答by Kent Boogaart
Couple of options:
几个选项:
- You can attach to
AppDomain.AssemblyResolve
to help the CLR resolve the assembly. - You could look into isolating add-ins into their own
AppDomain
(seeSystem.AddIn
namespace and this website).
- 您可以附加到
AppDomain.AssemblyResolve
以帮助 CLR 解析程序集。 - 您可以考虑将加载项隔离到它们自己的中
AppDomain
(请参阅System.AddIn
命名空间和本网站)。
回答by Chris Canal
Have you looked into using an Inversion Of Controlcontainer? I use Castle Windsor with an external Boo file that lets me easily extend the applcation without having to recompile or worry about supplying dependencies
您是否考虑过使用控制反转容器?我将 Castle Windsor 与外部 Boo 文件一起使用,这让我可以轻松扩展应用程序,而无需重新编译或担心提供依赖项
回答by Mark Cidade
You can use reflection to access the private Assembly.
_GetReferencedAssemblies()
.
您可以使用反射来访问私有Assembly.
_GetReferencedAssemblies()
.
Although, the method couldchange in a future version of the .NET framework, it doesn't seem likely—ASP.NET heavily depends on it, though it's possible they could move it from mscorlib
to System.Web
which is the only assembly that I know of from where the method is referred to.
虽然,该方法可能会在 .NET 框架的未来版本中发生变化,但似乎不太可能——ASP.NET 严重依赖它,尽管他们有可能将其移动mscorlib
到System.Web
我所知道的唯一程序集方法在哪里被引用。
回答by Olivier de Rivoyre
Assembly.LoadFrom works well until I try to use a webService in my addon, I had had a "Unable to cast object of type 'X' to type 'X'" exception.
在我尝试在插件中使用 webService 之前,Assembly.LoadFrom 运行良好,我遇到了“无法将类型为 'X' 的对象转换为类型为 'X' 的对象”异常。
It's ugly, but i will use Assembly.LoadFile with the AppDomain.AssemblyResolve.
这很丑陋,但我会将Assembly.LoadFile 与AppDomain.AssemblyResolve 一起使用。
Thanks guys.
谢谢你们。