0

我正在开发发票应用程序。

MEF过去采用模块化方法。

我创建了一个用于将文档打印到打印机的打印模块(插件)。

现在,我的问题是

一旦用户添加(保存)一个销售,就有一个名为销售的类。我需要检查是否安装了打印模块,如果是,那么我需要使用该模块将此销售传递给打印机。

我没有得到,我怎么能做到这一点。

我的想法是,我将[Import(typeof(IPrint))]在我的销售类中用作属性,如果我对这个属性有一些价值,那么我会将文档发送到打印机。

有没有更好的方法来满足这个要求。

4

1 回答 1

1

我不确定你是如何实现你的课程的。尝试这样的事情:

//in your Metadata you should have bool FireOnSave
public class Sales
{
    IEnumerable<Lazy<T, TMetadata>> myPlugins;
    private static CompositionContainer _container;
    static Sales()
    {
        var catalog = new AggregateCatalog();
        var d = new DirectoryInfo(".\\");
        catalog.Catalogs.Add(new DirectoryCatalog(d.FullName));
        _container = new CompositionContainer(catalog);
    }

    void ImportPlugins()
    {
         myPlugins = _container.GetExports<T, TMetadata>();
    }

    public void Save()
    {
        //do your saving work here
        fire_plugins();
    }

    private void fire_plugins()
    {
        foreach (var m in myPlugins)
        {
            if (m.Metadata.FireOnSave)
                m.Value.Invoke();
        }
    }
}

如果您有任何问题,请告诉我。

于 2013-04-16T21:19:42.940 回答