在 .Net 应用程序中引用程序集
在我的应用程序中,我想加载驻留在网络共享上的程序集(应用程序本身也位于该共享上),而不必将它们添加到项目的输出路径中。我想要的是一个除了应用程序之外几乎不包含任何内容的输出目录。
仅将目标目录添加到我的项目的引用路径中是行不通的。
我尝试在 App.Config 中设置探测目录,但这不起作用(甚至在我的本地磁盘而不是网络共享上测试了它)
提前感谢您的任何提示!
编辑:似乎这种情况是通过反思来引导的。是否有更优雅的方式使用像 NInject 这样的依赖注入容器?
In my application, I would like to load Assemblies that reside on a network share (The application itself is on that share, too) without having to add them to the project's output path. What I'd like to have is an output directory that contains next to nothing but the application.
Merely adding the target directory to my project's reference path's won't work.
I tried setting the probing directory in the App.Config, but that didn't work (even tested it on my local diks instead of the network share)
Thanks in advance for any hints!
EDIT : Seems the way this is going leads through reflection. Isn't there a more elegant way using Dependency Injection containers like NInject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,您无法将任意目录添加到探测路径:只能是应用程序中的子目录。
但是,可以使用
中的
元素提供程序集的完整路径。有一个例子
的 MSDN 页面。缺点是您需要显式列出应用程序引用的所有程序集的路径。另一种选择是在运行时通过
Assembly.LoadFrom
加载程序集并通过反射调用方法。如果您沿着这条路线走下去,则必须阅读 Suzanne Cook 的博客 。如果您没有阅读其他内容,请阅读 选择绑定上下文。Unfortunately you can't add an arbitrary directory to your probing path: only a subdirectory within your application.
However, it's possible to provide full paths to assemblies using the
<codeBase>
element within an<assemblyBinding>
. There's an example on the MSDN page for<assemblyBinding>
. The downside is that you'll need to explicitly list paths for all the assemblies your application references.Another option is to load assemblies at run time through
Assembly.LoadFrom
and invoke methods through reflection. If you go down this route, you must read Suzanne Cook's blog. If you read nothing else, read Choosing a Binding Context.我会看一下 Assembly.LoadFrom。您可以编写如下代码:
请注意,您还需要加载任何依赖程序集。
一旦你有了这个,你就可以使用以下方法创建对象:
并将其转换为 typeName 指定的类型
I'd take a look at Assembly.LoadFrom. You be able to write some code like this:
Note that you'll also need to load any dependant assemblies as well.
Once you have this you can create as object using:
And cast it to the type specified by typeName
如果我理解正确,您想从程序集中加载类而不必引用程序集的项目吗?
然后,您可以使用此类来获取实现/继承特定类型的所有类型:
之后,使用 John Sible 的答案来创建任何类型的对象即时
干杯!
If I understand correctly, you want to load classes from an assembly without having to reference the assembly's project ?
Then you could use this class to get all types that implements / inherits a certain type :
After that, use John Sibly's answer to create an object of any of the types on the fly
Cheers !