简体   繁体   English

共享DLL而不添加参考

[英]Sharing dll without adding reference

I have got a dll placed in a shared folder over development server. 我在开发服务器上的共享文件夹中放置了一个dll。 Is there any way to use that dll without adding reference in my application and without installing the same in GAC. 有什么方法可以使用该dll,而无需在我的应用程序中添加引用,也无需在GAC中安装该dll。

Thanks in advance. 提前致谢。

Assembly asm = Assembly.LoadFrom(path);

See MSDN for late binding, reflection etc. 有关后期绑定,反射等信息,请参见MSDN。

Small edit: A variable with the keyword "as" is asking for trouble. 小修改:带有关键字“ as”的变量正在引起麻烦。 So "Assembly as" changed to "Assembly asm" should be safer. 因此,将“ Assembly as”更改为“ Assembly asm”应该更安全。

You may want to look at the Managed Extensibility Framework or at Assembly.Load... in the base framework. 您可能要查看托管扩展框架或基本框架中的Assembly.Load...

Why would you want to do this, though? 但是,为什么要这样做呢? You'd need to call any code within the Assembly via reflection (hence the suggestion that the MEF may be what you're really after). 您需要通过反射调用程序集中的任何代码(因此,建议MEF可能就是您真正想要的)。

Yes, it is possible...somehow. 是的,有可能...以某种方式。 Have a look at the Assembly-Class . 看一下Assembly-Class With it you can load assemblies from a file without knowing what you exactly load. 有了它,您可以从文件中加载程序集,而无需知道确切地加载了什么。

Using Assembly.LoadFrom would be the only way to have zero references, but you'd still need to share contracts. 使用Assembly.LoadFrom是拥有零引用的唯一方法,但是您仍然需要共享合同。

What's the problem with adding a reference? 添加参考有什么问题?

What are you going to do when someone wants to work on a laptop and the WiFi goes down? 当有人想在笔记本电脑上工作并且WiFi掉线时,您该怎么办?

Yes, 是,

you can call Assembly.Load() and then make use of Reflection to call into the public interface (lowercase "interface" - what I mean is the methods, fields and properties) exposed by the assembbly. 您可以调用Assembly.Load(),然后利用Reflection调用由汇编公开的公共接口(小写的“接口”,我的意思是方法,字段和属性)。

But in order to do that you need to know what methods to call. 但是为了做到这一点,您需要知道调用什么方法。 It helps if you can be certain that the assembly includes classes that do conform to a known .NET interface . 如果可以确定程序集包含确实符合已知.NET 接口的类,则它将有所帮助。

This idea is the basis for "plug-in" architectures in many tools, where the tool loads any assembly in its "plugin" directory, instantiates classes, casts the result to an ISomething, and then invokes methods via that interface. 这个想法是许多工具中“插件”体系结构的基础,在该工具中,该工具将任何程序集加载到其“插件”目录中,实例化类,将结果转换为ISomething,然后通过该接口调用方法。

I also would read Suzanne Cook's .NET CLR Notes. 我还将阅读Suzanne Cook的.NET CLR注释。

http://blogs.msdn.com/suzcook/default.aspx http://blogs.msdn.com/suzcook/default.aspx

If this assembly is in a shared folder, you may find that .NET security restrictions stop you working with classes in that assembly in quite the way you'd expect. 如果此程序集位于共享文件夹中,则您可能会发现.NET安全限制使您无法按预期的方式使用该程序集中的类。

Rather than storing on a shared folder, you may want to consider checking in the assembly to your source code repository. 您可能需要考虑将程序集检入到源代码存储库中,而不是存储在共享文件夹中。 (I've seen a "/lib" folder used to good effect for this). (我已经看到一个“ / lib”文件夹用于此目的)。 Then you can reference the assembly directly. 然后,您可以直接引用程序集。

(There are also repository solutions such as Maven that can more properly control this. However, they don't play well with .NET, unfortunately.) (还有诸如Maven之类的存储库解决方案可以更适当地控制它。但是,不幸的是,它们不能与.NET很好地配合使用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM