通过代码添加参考?
我在 Visual Studio 2008 中有一个 C# .NET Compact Framework 3.5 移动项目。该程序使用条形码扫描仪,适用于两种不同类型的 Windows Mobile 设备(两者都使用自己的 SDK 来操作条形码扫描仪)。如何有条件地添加对扫描仪 SDK SDK DLL 文件的引用?即,如果针对 HARDWARE1 配置进行编译,我不想添加对 HARDWARE2-SCANNER-SDK.DLL 的引用。
I have a C# .NET Compact Framework 3.5 mobile project in Visual Studio 2008. The program uses a barcode scanner and works for two different types of Windows Mobile devices (both using their own SDK to manipulate the barcode scanner). How can I conditionally add the reference to the scanner SDK SDK DLL file? i.e., if compiling for HARDWARE1 configuration I don't want to add a reference to HARDWARE2-SCANNER-SDK.DLL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 app.config 文件中指定要加载的 DLL,然后使用 Assembly.Load 方法来加载正确的库(libraries)。
这就是简单的答案。但要真正完成这项工作,您可能需要创建一些共享通用 API(也在单独的库中)的“包装器”程序集,以便您的最终应用程序不必关心扫描仪信息的来源。它应该不知道正在使用什么硬件。
顺便说一句:这都与依赖注入的概念有关,尽管是在模块级别而不是类级别。以下是一些可帮助您入门的资源:
http://msdn.microsoft .com/en-us/magazine/cc163739.aspx
http://en.wikipedia.org/wiki/Dependency_injection
You can specify which DLL(s) to load in your app.config file and then use the Assembly.Load method to load the proper library(libraries.)
That's the simple answer. But to really make this work you might need to create a few "wrapper" assemblies that share a common API (also in a separate library) so that your final application will not have to care where your scanner info is coming from. It should be ignorant of what hardware is being used.
Btw: This is all related to the concept of dependency injection, albeit at the module rather than a class level. Here are a couple of resources to get you started:
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx
http://en.wikipedia.org/wiki/Dependency_injection
为什么需要这样做?如果您使用扫描仪访问接口,则一个实现将引用一个 SDK,而另一个实现将引用另一个 SDK。
在运行时,您实际创建的类将尝试加载引用的程序集,如果您在实例化之前检测硬件,则将加载正确的 SDK 引用。基本上,如果您不使用不存在的 SDK,那么它永远不会尝试加载它。
出现问题的唯一原因是两个 SDK 使用相同的类名。在这种情况下,我仍然会基于它的接口,但为接口的每个实现使用不同的 DLL,并且这些项目将引用其正确的 SDK。
Why do you need to? If you use an interface for the scanner access, then one implementation will reference one SDK and the other implementation will reference the other SDK.
At run time the class you actually create will attempt to load the referenced assembly and if you're detecting the hardware before instantiating then the proper SDK reference will get loaded. Basically if you don't use the SDK that isn't present, then it will never try to load it.
The only reason this would be a problem is if the two SDKs use the same class names. In that case I'd still interface base it, but have a different DLL for each implementation of the interface and those projects would reference their proper SDK.