C# 如何让plugin.dll调用核心应用程序的函数?
好的。所以我想建立一个程序,作为“插件”模块的核心。 另一个开发人员可以创建一个plugin1.dll并将其添加到“modules”文件夹中,以增强我的核心应用程序的功能 因此,让我们假设我的核心具有这些功能:C# 如何让plugin.dll调用核心应用程序的函数?,c#,plugins,reflection,C#,Plugins,Reflection,好的。所以我想建立一个程序,作为“插件”模块的核心。 另一个开发人员可以创建一个plugin1.dll并将其添加到“modules”文件夹中,以增强我的核心应用程序的功能 因此,让我们假设我的核心具有这些功能: 伐木 用户身份验证 用户界面 例如,我们有上面提到的核心应用程序,有人想添加一个插件,让用户看到当前时间并将其记录到一个standard-log.txt中 因此他将创建一个具有以下功能的类库: public interface ICoreApplication { //The
- 伐木
- 用户身份验证
- 用户界面
public interface ICoreApplication
{
//These are the methods that you want to provide to your plugins:
void LogMessage(string msg);
//void SomeOtherMethod(...)
//...
}
public interface IPlugin
{
//These are the methods that you expect from your plugins:
void Init(ICoreApplication coreReference);
}
public class Core : ICoreApplication
{
public void InitPlugins()
{
IPlugin somePlugin = ...; //retrieve via reflection
somePlugin.Init(this);
}
}
- 获取当前时间(包含在.dll中的功能)
- 显示当前时间(包含在.dll中的功能)
- 记录当前时间(核心中包含的功能)
我的plugin1.dll如何访问和调用核心程序的完整设置日志功能 我希望你明白我的问题。我希望我的plugin1.dll能够调用我的核心类的示例日志记录方法
谢谢 我建议将您的问题分为两个方面:
public interface ICoreApplication
{
//These are the methods that you want to provide to your plugins:
void LogMessage(string msg);
//void SomeOtherMethod(...)
//...
}
public interface IPlugin
{
//These are the methods that you expect from your plugins:
void Init(ICoreApplication coreReference);
}
public class Core : ICoreApplication
{
public void InitPlugins()
{
IPlugin somePlugin = ...; //retrieve via reflection
somePlugin.Init(this);
}
}
(顺便说一句:如果你知道你希望你的插件提供的功能,IPlugin接口也可以包含其他方法。在这种情况下,你不需要通过反射来调用插件,而是通过这个接口。)
核心应用程序:
public interface ICoreApplication
{
//These are the methods that you want to provide to your plugins:
void LogMessage(string msg);
//void SomeOtherMethod(...)
//...
}
public interface IPlugin
{
//These are the methods that you expect from your plugins:
void Init(ICoreApplication coreReference);
}
public class Core : ICoreApplication
{
public void InitPlugins()
{
IPlugin somePlugin = ...; //retrieve via reflection
somePlugin.Init(this);
}
}
请注意,这只是一个简单的示例,用于说明基本概念。提供一个健壮的插件架构还有很多。你需要考虑像这样的事情
- 安全性(你能信任你的插件吗?你能信任你加载它们的文件系统吗?)
- 错误处理(如果插件抛出异常会发生什么?如果插件想要通知您“预期失败”会发生什么?)
- 线程(如果在主线程上调用插件,它们可能会阻止整个应用程序。如果在其他线程上调用插件,则需要考虑同步。如果插件创建新线程并在该线程上调用核心应用程序,该怎么办?)
- 等等