简体   繁体   中英

How to group voids in c# MEF Plugins

How can I group voids in c# MEF?

I have the main plugin interface:

[InheritedExport]
public interface IHostPlugin
{
    void LOG_WriteLog(string Message);
    void LOG_DeleteLog(string Id);
    void Notifications_SendNotification(string Message);
}

Now, on the plugin

[Import("IHostPlugin", typeof(IHostPlugin))]
public IHostPlugin HostPlugin { get; set; }

and I can call the plugin Void like this:

HostPlugin.LOG_WriteLog("Test");

But what I want is to group the voids, and call like this:

HostPlugin.LOG.WriteLog("Test");
HostPlugin.Notifications.SendNotification("Test");

Is it possible?

Thanks

I think your best shot is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM