C# 从DLL调用函数
众所周知,您可以在dll中包含函数,引用dll,然后从主可执行文件调用函数。 我想知道反向的方法是否也可行? 因此,我喜欢从dll中调用主可执行文件中的函数,而不需要在dll中调用实际的函数。C# 从DLL调用函数,c#,vb.net,function,dll,reference,C#,Vb.net,Function,Dll,Reference,众所周知,您可以在dll中包含函数,引用dll,然后从主可执行文件调用函数。 我想知道反向的方法是否也可行? 因此,我喜欢从dll中调用主可执行文件中的函数,而不需要在dll中调用实际的函数。 原因:我正在开发一个插件系统。你在比较苹果和橙子:构建系统引用dll与插件系统完全不同,插件系统中的所有事情都发生在运行时。通常,您希望从插件主机(您的exe)调用某些函数的插件系统如下(简化): 是的,可执行文件可以作为引用添加到项目中,并且可以使用与从引用的DLL调用函数相同的方法 //in a co
原因:我正在开发一个插件系统。你在比较苹果和橙子:构建系统引用dll与插件系统完全不同,插件系统中的所有事情都发生在运行时。通常,您希望从插件主机(您的exe)调用某些函数的插件系统如下(简化):
是的,可执行文件可以作为引用添加到项目中,并且可以使用与从引用的DLL调用函数相同的方法
//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 );