添加插件的支持:接口或基类继承?插件、接口

2023-09-04 02:58:55 作者:孓曰.尐孑亥孓嗼說愛

我添加的插件支持我的.NET应用程序。基类听起来很有道理对我来说,这样的人可以继承它和覆盖的特定呼叫,以及可以保持默认功能还是可以使用一些内置的辅助功能。

I'm adding plugin support to my .NET application. A base class sounds reasonable to me, so people can inherit it and override certain calls as well can keep the default functionality or can use some internal helper functions.

为什么我会选择界面,而不是一个基类的插件来继承?你能告诉它的设计,我应该选择?为什么?

Why would I choose interface instead of a base plugin class to inherit? Could you tell which design I should choose and why?

推荐答案

您应该考虑采取一看System.Addin(MAF)命名空间或托管扩展框架(MEF)。 MEF将是preferred选择,即使它仍然是$ P $对 - 释放,因为它比MAF简单得多。无论是那些选择之一简化了很多的管道工作,你需要做的,以获得插件加载并与之互动。

You should consider taking a look at the System.Addin (MAF) namespace or the Managed Extensibility Framework (MEF). MEF would be the preferred choice even though it is still pre-release as it is much simpler than MAF. Either one of those choices simplify a lot of the plumbing work you will need to do in order to get add-ins loaded and interact with them.

至于做选择的接口和抽象类的,如果你去与MAF或MEF一些会为你进行。在这方面,最根本的区别是,通过提供一个抽象基类为自定义插件(无论是由您或其他开发人员编写的)提供了一个传承点,确保某些方法/属性提供默认行为,并迫使派生类来实现某些所需的方法/属性。其缺点是,你是局限于单一基类。

As far as making a choice between an interface and an abstract class, if you go with MAF or MEF some of that will be made for you. The most basic differences in this context are that by providing an abstract base class you provide an inheritance point for custom plugins (written either by you or other developers) and ensure that certain methods/properties provide default behavior and force the derived classes to implement certain required methods/properties. The drawback is that you are limited to a single base class.

接口避开单个基类的限制,仍然提供一个inhertiance点定制插件和确保某些方法/属性实现,但不能提供任何类型的默认行为。你也不能指定以外的任何一个接口公共成员等,而抽象类可以有抽象的或虚拟的非公共成员(一般保护)。

Interfaces get around the single base class limitation and still provide an inhertiance point for custom plugins and ensure that certain methods/properties are implemented but can't provide any type of default behavior. You also cannot specify anything other than public members in an interface while an abstract class can have abstract or virtual non-public members (typically protected).