如何对c#MEF插件中的空白进行分组
如何在c#MEF中对空进行分组 我有主插件接口:如何对c#MEF插件中的空白进行分组,c#,mef,C#,Mef,如何在c#MEF中对空进行分组 我有主插件接口: [InheritedExport] public interface IHostPlugin { void LOG_WriteLog(string Message); void LOG_DeleteLog(string Id); void Notifications_SendNotification(string Message); } 现在,关于插件 [Import("IHostPlugin", t
[InheritedExport]
public interface IHostPlugin
{
void LOG_WriteLog(string Message);
void LOG_DeleteLog(string Id);
void Notifications_SendNotification(string Message);
}
现在,关于插件
[Import("IHostPlugin", typeof(IHostPlugin))]
public IHostPlugin HostPlugin { get; set; }
我可以这样调用插件Void:
HostPlugin.LOG_WriteLog("Test");
HostPlugin.LOG.WriteLog("Test");
HostPlugin.Notifications.SendNotification("Test");
但我想要的是将这些空洞分组,并这样称呼:
HostPlugin.LOG_WriteLog("Test");
HostPlugin.LOG.WriteLog("Test");
HostPlugin.Notifications.SendNotification("Test");
可能吗
谢谢我想你最好的办法是:
internal static class Program
{
private static void Main(string[] args)
{
TypeCatalog catalog = new TypeCatalog(typeof(LOG), typeof(Notifications), typeof(Plugin1));
CompositionContainer container = new CompositionContainer(catalog);
HostPlugin host = new HostPlugin();
container.SatisfyImportsOnce(host);
host.LOG.WriteLog("Test");
host.Notifications.SendNotification("Test");
}
}
public class Plugin1
{
[Export("LOG.WriteLog")]
private void LOG_WriteLog(string Message) { }
[Export("LOG.DeleteLog")]
private void LOG_DeleteLog(string Id) { }
[Export("Notifications.SendNotification")]
private void Notifications_SendNotification(string Message) { }
}
public class HostPlugin
{
[Import]
public LOG LOG { get; private set; }
[Import]
public Notifications Notifications { get; private set; }
}
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class LOG
{
[ImportMany("LOG.WriteLog", AllowRecomposition = true)]
private Action<string>[] Write { get; set; }
public void WriteLog(string Message)
{
foreach (Action<string> action in Write)
{
action(Message);
}
}
[ImportMany("LOG.DeleteLog", AllowRecomposition = true)]
private Action<string>[] Delete { get; set; }
public void DeleteLog(string Id)
{
foreach (Action<string> action in Delete)
{
action(Id);
}
}
}
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Notifications
{
[ImportMany("Notifications.SendNotification", AllowRecomposition = true)]
private Action<string>[] Send { get; set; }
public void SendNotification(string Message)
{
foreach (Action<string> action in Send)
{
action(Message);
}
}
}
内部静态类程序
{
私有静态void Main(字符串[]args)
{
TypeCatalog catalog=新的TypeCatalog(日志类型、通知类型、插件类型);
CompositionContainer=新的CompositionContainer(目录);
HostPlugin主机=新的HostPlugin();
集装箱。满足进口(主机);
host.LOG.WriteLog(“测试”);
host.Notifications.SendNotification(“测试”);
}
}
公共类插件1
{
[导出(“LOG.WriteLog”)]
私有无效日志_WriteLog(字符串消息){}
[导出(“LOG.DeleteLog”)]
私有无效日志\u DeleteLog(字符串Id){}
[导出(“Notifications.SendNotification”)]
私有无效通知\u发送通知(字符串消息){}
}
公共类主机插件
{
[进口]
公共日志日志{get;private set;}
[进口]
公共通知{get;private set;}
}
[出口]
[PartCreationPolicy(CreationPolicy.Shared)]
公共类日志
{
[ImportMany(“LOG.WriteLog”,AllowRecomposition=true)]
私有操作[]写入{get;set;}
公共空写日志(字符串消息)
{
foreach(书面操作)
{
行动(信息);
}
}
[ImportMany(“LOG.DeleteLog”,AllowRecomposition=true)]
私有操作[]删除{get;set;}
公共void DeleteLog(字符串Id)
{
foreach(删除中的操作)
{
行动(Id);
}
}
}
[出口]
[PartCreationPolicy(CreationPolicy.Shared)]
公共类通知
{
[ImportMany(“Notifications.SendNotification”,AllowRecomposition=true)]
私有操作[]发送{get;set;}
公共无效发送通知(字符串消息)
{
foreach(发送中的操作)
{
行动(信息);
}
}
}