【发布时间】:2014-04-22 09:25:04
【问题描述】:
众所周知,您可以在 dll 中拥有函数,引用该 DLL,然后从您的主可执行文件中调用这些函数。 我想知道相反的方式是否也可以? 所以我喜欢从 dll 调用主可执行文件中的函数,而不需要在 dll 中调用的实际函数。 原因:我正在开发一个插件系统。
【问题讨论】:
标签: c# vb.net function dll reference
众所周知,您可以在 dll 中拥有函数,引用该 DLL,然后从您的主可执行文件中调用这些函数。 我想知道相反的方式是否也可以? 所以我喜欢从 dll 调用主可执行文件中的函数,而不需要在 dll 中调用的实际函数。 原因:我正在开发一个插件系统。
【问题讨论】:
标签: c# vb.net function dll reference
是的,可执行文件可以作为引用添加到您的项目中,您可以使用它们的方式与从引用的 dll 中调用函数的方式相同
【讨论】:
您有点像是在比较苹果和橙子:构建系统引用 dll 与插件系统完全不同,插件系统中的一切都发生在运行时。通常,您希望从插件主机(您的 exe)调用某些功能的插件系统将如下所示(简化):
//in a common project
//functions from the host that will be callable by the plugin
public interface PluginHost
{
void Foo();
}
//the plugin
public interface Plugin
{
void DoSomething( PluginHost host );
}
//in the exe
class ThePluginHost : PluginHost
{
//implement Foo
}
//in the plugin
class ThePlugin : Plugin
{
//implement DoSomething,
//has access to exe methods through PluginHost
}
//now al that's left is loading the plugin dll dynamically,
//and creating a Plugin object from it.
//Can be done using Prism/MEF etc, that's too broad of a scope for this answer
PluginHost host = new ThePluginHost();
Plugin plugin = CreatePluginInstance( "/path/to/dll" );
plugin.DoSomething( host );
【讨论】: