ImportMany返回从接口派生的所有类

时间:2018-12-11 11:40:42

标签: c# .net mef

团队, 我们已经实现了MEF(此概念的新功能)。我们的结构是 界面

 public interface IAction
{
    string DoPostUpdate(BaseSchema baseClass);

} 

创建一个抽象类为

   [Export(typeof(IAction))]
public abstract class BaseActions : IAction
{
    public virtual string DoPostUpdate(BaseSchema baseClass)
    {
        return "Inside abstract method";
    }
}

以及从该抽象类派生的两个类

[Export(typeof(IAction))]

public class GroupActions : BaseActions
{
    public override string DoPostUpdate(BaseSchema baseClass)
    {
        return "Inside Group Action export";
    }

}

[Export(typeof(IAction))]
    public class PartyActions : BaseActions
    {
        public override string DoPostUpdate(BaseSchema baseClass)
        {

            return "Inside Party Action export";
        }
    }

容器初始化是在单独的项目中完成的-

 private CompositionContainer _container;
    [ImportMany(typeof(IAction))]
    public List<IAction> actionContract;

        if (actionContract == null)
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(T).Assembly));
            catalog.Catalogs.Add(new DirectoryCatalog(Settings.Default.ActionPluginPath));
            _container = new CompositionContainer(catalog);

            try
            {
                this._container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                //  Console.WriteLine(compositionException.ToString());
            }
        }

现在初始化发生后actionContract对象将两个类对象都包含在其中。

现在的问题是,如何仅在此结构中调用PartyAction类或GroupAction类的DoPostUpdate

你能指导吗?

0 个答案:

没有答案