支持非托管 DLL 的多个版本的 .NET 客户端

发布于 2024-10-28 06:12:58 字数 684 浏览 1 评论 0原文

我正在开发一个 .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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-11-04 06:12:58

无论是托管代码还是本机代码,都没有干净的方法来解决此问题。您能做的最好的事情就是声明一个接口类型,尝试覆盖所有可能的版本,然后为每个单独的 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.

云胡 2024-11-04 06:12:58

您可以加载不同版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文