支持非托管 DLL 的多个版本的 .NET 客户端
我正在开发一个 .NET 4.0 客户端,它将利用 C 库进行数据处理。用户将能够指定他们希望加载进行处理的 DLL 文件。
我正在按照此处所述进行后期绑定/程序集加载。 http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanagement-dll-from-.net-_2800_c_23002900_.aspx
对于每个 DLL,相同的方法调用序列在我的客户端中将是相同的,但方法签名会改变或者传入的数据结构会改变。根据 DLL 的版本和其他因素,用结构填充的数据会有所不同。例如,MyStruct 的定义将根据 DLL 的版本而变化。
public delegate int INTF_my_method(ref MyStruct pDataStruct);
对于这种方法,建议使用哪些设计模式或设计决策?我需要根据用户指定的 DLL 版本加载适当的 C 方法委托和数据定义,并适当地填充结构。以前有人做过这样的事情吗?
I am developing a .NET 4.0 client that will utilize a C Library for data processing. The user will be able to specify the DLL file they wish to load for processing.
I am doing late binding / assembly loading as described here. http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx
For each DLL, the same method call sequences will be the same in my client, but the method signatures will change or the data structs passed in will change. The data populated with the structures will be different depending on the version of the DLL and other factors. Example, the definition of MyStruct will change depending on the version of the DLL.
public delegate int INTF_my_method(ref MyStruct pDataStruct);
What design patterns or design decision are recommended for this approach? I need to load the appropriate C method delegates and data definitions based on the version of the DLL that the user has specified, and populate the structures appropriately. Has anyone done something like this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论是托管代码还是本机代码,都没有干净的方法来解决此问题。您能做的最好的事情就是声明一个接口类型,尝试覆盖所有可能的版本,然后为每个单独的 API 版本编写具体的包装类。如果至少有一些通用功能,那么您可以将其铲入基类中。
还值得注意的是,您不能只让用户选择一个 DLL,您必须将该 DLL 与具体的包装类实例配对。
在程序中构建这种灵活性显然是非常昂贵的。
There is no clean approach to this, neither in managed code nor native code. The best you could possibly do is to declare an interface type that tries to cover all possible versions and then write concrete wrapper classes for each individual version of the API. If there's at least some common functionality then you can shovel that in a base class.
Notable too is that you cannot just let the user pick a DLL, you have to pair the DLL with the concrete wrapper class instance.
Building this kind of flexibility in your program is obviously very expensive.
您可以加载不同版本的 DLL,但只能从单独的 AppDomain 加载。也就是说,对于要加载的每个 DLL,您都必须创建一个新的 AppDomain。
You can load different versions of your DLLs, but only from separate AppDomains. That is, for each DLL you want to load, you will have to create a new AppDomain.