将引用动态添加到我的项目中
我正在为 Microsoft Word 开发插件。 在我的加载项中,我使用对文件“Microsoft.Office.Interop.Word.dll”的引用,以便我可以使用 Word 对象。
如果开发机器安装了word 2007,则引用中要包含的文件是版本12 如果安装了 Word 2010,该文件应该是版本 14。
我如何构建我的插件并使其工作,无论计算机上安装了哪个版本的 Word(换句话说,告诉我的应用程序自动检测哪个版本的 Office)是否已安装并自动使用适当的互操作文件)?
I am Developping an Add-In for Microsoft Word.
In my Add-In i am using a reference to file "Microsoft.Office.Interop.Word.dll" so i can use word objects.
In case the development machine has word 2007 installed, the file to be included in references is version 12
in case word 2010 is installed, the file should be version 14.
how can i build my add in and make it work regardless of which version of Word is installed on the machine (in other word tell my application to automatically detect which version of office is installed and use the appropriate interop file automatically)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的实际代码正在引用给定程序集中的项目,则尝试挂钩不同的版本实际上可能会导致错误(例如,如果某些方法签名已更改)。在大多数情况下,最好的策略是生成不同的版本来针对不同的 DLL 版本,并在用户的计算机上安装正确的版本。
您可以使用一些策略来使这变得更容易,例如将对此库的调用包装在一组接口中,然后使用两个主要的“插件”程序集来提供接口的实现,但在实现它们时针对不同的 DLL。该软件可以通过配置动态加载实现绑定,类似于流行的依赖注入框架的方式。这样,如果方法签名从一个版本更改为另一个版本,您只需更改一个类,其余代码仍将按原样工作。
If your actual code is making reference to the items in the given assembly, trying to hook into a different version could actually cause errors (e.g. if certain method signatures have been changed). In most cases, the best strategy would be to produce different builds to target the different DLL versions, and install the correct build on the user's machine.
There are strategies you can use to make this easier, like wrapping your calls to this library in a set of interfaces, and then having two main "plugin" assemblies that provide implementations for the interfaces, but which target different DLLs when they implement them. The software can load the implementation bindings dynamically by configuration, similar to the way popular Dependency Injection frameworks do. That way, if a method signature changes from one version to the other, you can just change a single class and the rest of your code will still work as-is.