如何使用我没有的 C#(托管)dll?
如何正确引用我无权物理访问的 C# DLL? (我实际上有 DLL,只是不是它的所有依赖项。)
背景:
在目标计算机上,C:\FancyProgram
中安装了一个程序和许多相互依赖的 dll 文件。
我需要编写一个简单的应用程序,将其部署到目标计算机上的 C:\SimpleProgram
中。
这个简单的程序需要对 C:\FancyProgram
下的 dll 文件之一进行一些调用。它是一个 .net 管理的 DLL
在我的开发计算机上,我没有 FancyProgram
的副本。我确实有那个 DLL 文件,但我没有它的许多依赖项中的任何一个。我也无法将该 DLL 捆绑到 SimpleProgram
中。
在我的 Visual Studio 项目中使用和引用此 DLL 的正确方法是什么,以便我的 SimpleProgram
能够正确编译但在运行时从正确的位置加载 DLL?
谢谢
How can I correctly reference a C# DLL that I don't have physical access to? (I actually have the DLL, just not all it's dependencies.)
Background:
On the target computer, there is a program and many interdependent dll files installed in C:\FancyProgram
.
I need to write a simple application that will be deployed on the target computer to C:\SimpleProgram
.
This simple program will need to make some calls into one of the dll files under C:\FancyProgram
. It is a .net managed DLL
On my development machine, I do not have a copy of FancyProgram
. I do have that one DLL file, but I do not have any of it's many dependencies. I also can not bundle that DLL into SimpleProgram
.
What is the correct method to use and reference this DLL in my Visual Studio project such that my SimpleProgram
will correctly compile but load the DLL at runtime from the correct location?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的建议是为您想要从该 dll 使用的功能创建一个 Facade。并且不要直接使用(引用)它 - 动态解析并加载它:
C# - 加载程序集的正确方法,查找类并调用Run()方法
在运行时加载程序集并创建类实例
My recommendation is to create a Facades for the functionality you want to use from that dll. And don't use (reference) it directly - resolve and load it dynamically:
C# - Correct Way to Load Assembly, Find Class and Call Run() Method
Load Assembly at runtime and create class instance
.Net 无论如何都会进行后期绑定。只要您在真正要加载 .dll 之前不以任何方式引用它,这就会起作用。
只需将所有引用(字段、属性、方法、实例)封装到单独的类或项目中,并仅在必要时创建实例。然后您可以尝试捕获加载错误。
当您的应用程序运行时,请参阅 Visual Studio 输出窗口,它会告诉您何时尝试加载什么 .dll。
您可能还想查看这些事件,以使您的应用程序能够优雅地处理错误:
您可能还想采用 MEF 方法。它是一个用于进行后期加载/绑定的框架。
.Net will do late binding anyway. As long as you don't reference the .dll in any way until you actually mean to load it this will work.
Simply encapsulate all references (fields, properties, methods, instances) into a spearate class or project and make an instance only when you have to. You can then try-catch the load error.
See Visual Studio Output window when your app is run, it will tell you when its is attempting to load what .dll.
You may also want to look at these events to make your app handle errors gracefully:
You may also want to take the MEF approach. It is a framework for doing late loading/binding.
您可能想查看
LoadLibrary
、GetProcAddress
和Marshal.GetDelegateForFunctionPointer
的详细信息。我还会构建用于本地测试的 dll,其接口与外部 dll 相同。您在其中放置多少功能取决于界面和您的
SimpleProgram
的复杂性。对于我关于导入外部 dll 的旧问题,有一些很好的答案。
You might want to look at the details of
LoadLibrary
,GetProcAddress
andMarshal.GetDelegateForFunctionPointer
.I would also build dll for testing locally, with the same interface as your external dll. How much functionality yo put in there depends on the complexity of the interface and your
SimpleProgram
.There were some excellent answers to my old question about importing external dlls.
呃呃呃,你打算测试什么?假设您已经弄清楚了,您需要确保 .dll 位于 GAC 中并以这种方式引用它(理想情况下)或者 .dll 需要位于所有计算机上的同一位置。在引用中添加 .dll 并将其标记为 Copy Local: false,这样您就不会部署它。祝你好运。
Uhhhh, what are you going to do about testing it? Assuming you've figured that out you need to make sure that either the .dll is in the GAC and reference it that way (ideally) or the .dll needs to be in the same place on all computers. Add the .dll in your references and mark it as Copy Local: false, so you don't deploy it. Good luck.