在 C# .NET 2.0 中实现插件架构,无需额外的接口库

发布于 2024-12-14 03:08:21 字数 143 浏览 4 评论 0原文

我必须为我的应用程序实现一个插件架构。

我有需要在运行时加载的主应用程序和库。我需要我的主应用程序了解 dll 中定义的类,以便在动态调用期间正确处理它们。

我是否需要使用接口实现额外的 dll 来实现此目标,还是有其他方法可以实现此目标?

I have to implement a plugin architecture for my application.

I have the main application and library that I need to load at runtime. I need my main application to know about classes defined in dll to correctly process them during the dynamic call.

Am I need to implement additional dll with Interface to achieve this goal or It is some other way doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

在风中等你 2024-12-21 03:08:21

您可以使用 Assembly.Load 加载库,然后您需要一种方法来检测需要实例化的类型。您可以使用接口(即 IPlugin)标记类,然后查看是否可以使用 IsAssignableFrom 方法将您正在检查的类型分配给 IPlugin 类型。另一种方法是使用自定义属性标记您的类,并查看您正在检查的类型是否具有该属性(即 PluginAttribute)。在这两种情况下,您都需要将接口或属性类放入第三个库中,以便可以在主应用程序和插件库中引用它。

You can load the library using Assembly.Load, and then you need a way to detect which types you need to instantiate. You can mark classes with an interface (i.e. IPlugin) and then see if the type you are inspecting can be assigned to the IPlugin type using the IsAssignableFrom method. Another approach is to mark your classes with custom attributes and see if the type you are inspecting has that attribute (i.e. PluginAttribute). In both cases you need to put the interface or the attribute class in a third library so it can be referenced in the main application and in the plugin library.

无畏 2024-12-21 03:08:21

您可以使用 Assembly.Load,然后使用反射来检查共享接口/属性的类型。

You can use Assembly.Load, and then use reflection to check the types for your shared interface / attribute.

深空失忆 2024-12-21 03:08:21

您是否考虑过托管扩展性框架?该链接还有一个很好的示例

Have you considered Managed Extensibility Framework ? The link also has a nice sample

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