仅在加载 (.NET) DLL 时才执行功能的最佳方法? [英] Best way to only perform a function if a (.NET) DLL is loaded?

查看:15
本文介绍了仅在加载 (.NET) DLL 时才执行功能的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定解释这一点的最佳方式,所以如果您不明白,请发表评论.

I am not sure the best way to explain this so please leave comments if you do not understand.

基本上,我有一些库用于处理不同程序的各种任务——通知只是一个例子.

Basically, I have a few libraries for various tasks to work with different programs - notification is just one example.

现在,我正在构建一个新程序,我希望它尽可能轻巧.虽然我想包含我的通知引擎,但我认为实际上没有多少人会使用它的功能,因此,我宁愿默认不包含它 - 只是作为可选下载.

Now, I am building a new program, and I want it to be as lightweight as possible. Whilst I would like to include my notification engine, I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.

我将如何编程?

使用非托管 Dlls 和 P/Invoke,我基本上可以尝试将所有内容打包/catch 循环,但我不确定托管版本.

With unmanaged Dlls and P/Invoke, I can basically wrap the whole lot in a try/catch loop, but I am not sure about the managed version.

到目前为止,我能想到的最好方法是在启动时检查 DLL 文件是否存在,然后设置一个字段 bool 或类似字段,并且每次我想要触发通知时,我都可以执行 if/check布尔和火...

So far, the best way I can think of is to check if the DLL file exists upon startup then set a field bool or similar, and every time I would like a notification to be fired, I could do an if/check the bool and fire...

我从调试窗口看到 DLL 文件仅在需要时加载.该程序显然可以编译,因为所有组件对项目都是可见的,但它会在没有 DLL 的最终用户机器上运行吗?

I have seen from the debug window that DLL files are only loaded as they are needed. The program would obviously compile as all components will be visible to the project, but would it run on the end users machine without the DLL?

更重要的是,有没有更好的方法来做到这一点?

More importantly, is there a better way of doing this?

理想情况下,我希望我的应用程序中没有任何关于通知的内容,并且以某种方式拥有它,以便在下载 DLL 文件时,它会在外部添加此功能.有一些额外的字节调用 notification("blabla");(或类似的)确实不是世界末日,但是当我有更大的意图,只想了解此类事情的最佳做法.

I would ideally like to have nothing about notifications in my application and somehow have it so that if the DLL file is downloaded, it adds this functionality externally. It really is not the end of the world to have a few extra bytes calling notification("blabla"); (or similar), but I am thinking a lot further down the line when I have much bigger intentions and just want to know best practices for this sort of thing.

推荐答案

我不认为很多人会实际使用它的功能,所以,我宁愿不默认包含它- 仅作为可选下载.

I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.

这些东西通常被描述为插件(或附加组件或扩展).

Such things are typically described as plugins (or add-ons, or extensions).

从 .NET 4 开始,标准方法是使用 托管扩展性框架.它作为 System.ComponentModel.Composition 程序集和命名空间包含在框架中.首先,最好阅读MSDN文章MEF 编程指南.

Since .NET 4, the standard way to do that is with the Managed Exensibility Framework. It is included in the framework as the System.ComponentModel.Composition assembly and namespace. To get started, it is best to read the MSDN article and the MEF programming guide.

这篇关于仅在加载 (.NET) DLL 时才执行功能的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆