Alf*_*fie 5 c# plugins extensibility mef
我正在尝试使用托管扩展框架(MEF)向我的C#应用程序添加插件可扩展性框架到目前为止它还可以; 我有我的主/主机应用程序从定义的文件夹加载插件,并可以从主应用程序调用他们的方法等.宿主应用程序和插件都引用了一个单独的dll程序集,其中包含所有项目通用的接口.
这工作正常,我可以从主应用程序调用/与插件交互.不过,我也希望能够与宿主应用程序进行交互,从插件中,但似乎无法了解如何完成此操作.
我希望能够从我的插件中获取/设置/执行主应用程序中的导出属性和方法.目前我只能从主应用程序"说出"插件,而不是相反.
我的代码到目前为止:
接口DLL
namespace MefContracts
{
[InheritedExport]
public interface IPlugin
{
String DoWork();
}
public class Class1
{
public IPlugin plugin { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
主要/主机应用程序
namespace MyMEF
{
class clsMEF
{
private CompositionContainer _container;
[Import(typeof(MefContracts.IPlugin))]
public MefContracts.IPlugin plugin;
public clsMEF()
{
Compose();
}
void Compose()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("..\\..\\Extensions"));
_container = new CompositionContainer(catalog);
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
}
void Main()
{
clsMEF myMef = new clsMEF();
MessageBox.Show(myMef.plugin.DoWork());
}
}
Run Code Online (Sandbox Code Playgroud)
插入
namespace MefPlugin
{
[Export]
public class Class1 : MefContracts.IPlugin
{
public String DoWork()
{
return "Plugin called";
}
}
}
Run Code Online (Sandbox Code Playgroud)
经过多次尝试和错误,我发现我遇到的问题是我没有将当前执行的程序集 ( System.Reflection.Assembly.GetExecutingAssembly()) 与插件的程序集一起添加到主机的程序集目录中。
非常感谢@PanosRontogiannis,他让我走上了正确的道路——一旦正确添加了程序集,这个答案就非常有效。
这是其他有需要的人的工作代码:
接口动态链接库
using System.ComponentModel.Composition;
namespace MefContracts
{
[InheritedExport]
public interface IPlugin
{
String Work(String input);
}
[InheritedExport]
public interface IHost
{
string Version { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
主机应用
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace MyMEF
{
[Export]
public class Class1 : MefContracts.IHost
{
public String Version
{
get { return "v1.00"; }
}
}
class clsMEF
{
private CompositionContainer _container;
[Import(typeof(MefContracts.IPlugin))]
public MefContracts.IPlugin plugin;
public clsMEF()
{
Compose();
}
void Compose()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("..\\..\\Extensions"));
catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly())); // <-- THIS WAS THE MISSING PIECE
_container = new CompositionContainer(catalog);
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
}
static class Program
{
static void Main()
{
clsMEF myMef = new clsMEF();
MessageBox.Show(myMef.plugin.Work("My Input"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
插入
using System.ComponentModel.Composition;
namespace MefPlugin
{
[Export]
public class Class2 : MefContracts.IPlugin
{
[Import(typeof(MefContracts.IHost))]
public MefContracts.IHost Host;
public String Work(String input)
{
return "Plugin Called (Input: " + input + "). Host Application Version: " + input + Host.Version;
}
}
}
Run Code Online (Sandbox Code Playgroud)