C#中的定义/实现问题
我正在尝试创建这样一个插件架构:
IPlugin:所有插件都必须实现的接口。
NormalPlugin:普通插件。
ComplexPlugin:一个插件,除了实现基本方法之外,还具有一些自定义方法。
HostApp:一个知道 IPlugin 是什么的应用程序。
目前,主机应用程序将在编译时拥有 ComplexPlugin 并将动态加载 NormalPlugin。这是因为主机需要在 ComplexPlugin 中有定义,以便它可以调用它的自定义方法。
1) 有没有更好的方法来实现这一目标?因为在编译时添加一些插件作为对主机应用程序的引用对我来说看起来有点蹩脚。
2) 我尝试使用:
public interface IPlugin
{
object CallCustomMethod(string methodName, object[] parameters);
}
but still, if CallCustomMethod returns a complex type, the app will need to know that complex type to cast to. thanks in advanceI am trying create such a plugin architecture that;
IPlugin: an interface that all plugins must implement.
NormalPlugin: a normal plugin.
ComplexPlugin: a plugin which, beside implementing the base methods, has some custom methods.
HostApp: an app that knows what an IPlugin is.
currently the host app will have ComplexPlugin at compile-time and will load the NormalPlugin dynamically. That's because host needs to have definitions in ComplexPlugin so it can call it's custom methods.
1) Are there any better methods for achieving this? because adding some plugins as reference to the host app at compile-time looks a little lame to me.
2) I tried using:
public interface IPlugin
{
object CallCustomMethod(string methodName, object[] parameters);
}
but still, if CallCustomMethod returns a complex type, the app will need to know that complex type to cast to.
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果应用程序除了通过 CallCustomMethod 函数之外从未调用 ComplexPlugIn 的自定义方法,那么在我看来,您不需要转换为 ComplexPlugIn。
IPlugIn foo = (IPlugIn)myComplexObject.CallCustomMethod("GenerateNewComplexPlugIn", new object[] { 0 });
foo.CallCustomMethod("AnotherComplexMethod", null);
If the the app never calls ComplexPlugIn's custom methods other than through your CallCustomMethod function, then it seems to me that you shouldn't ever need to cast to a ComplexPlugIn.
IPlugIn foo = (IPlugIn)myComplexObject.CallCustomMethod("GenerateNewComplexPlugIn", new object[] { 0 });
foo.CallCustomMethod("AnotherComplexMethod", null);