簡體   English   中英

如何在 c# MEF 插件中對空洞進行分組

[英]How to group voids in c# MEF Plugins

如何在 c# MEF 中對空洞進行分組?

我有主插件界面:

[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");

可能嗎?

謝謝

我認為你最好的鏡頭是:

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM