簡體   English   中英

在MEF中導出/導入接口

[英]Exporting/Importing Interfaces in MEF

請參閱以下代碼。 使用MEF我創建了一個管理器對象和一個插件,它可以導入IQueryEngine。

但是,當我在插件中調用方法_queryEngine.Get()時,我得到一個MissingMethodException,就像在QueryEngine的具體實現中沒有實現該方法一樣,但是它已經實現了。 我相信這與ManagedElementDTO類有關,但我不知道該怎么做。

任何人都可以了解正在發生的事情嗎?

我有一個界面:

public interface IQueryEngine
{
    ManagedElementDTO Get();
}

並實施:

public class QueryEngine : IQueryEngine
{
    public ManagedElementDTO Get() 
    {
        return new ManagedElementDTO();
    }
}

該引擎在經理中聲明:

[Export]
public class ProviderSupervisor : ISupervisor
{
    [Export("QueryEngine")]
    private IQueryEngine _queryEngine;

    [ImportMany(typeof(IProviderModule))]
    private IEnumerable<IProviderModule> _providers; 

    public ProviderSupervisor(IStore store)
    {
        _queryEngine = new QueryEngine();

        CreateContainer();
    }

    /// <summary>
    /// Creates the composition container and composes the parts.
    /// </summary>
    /// <returns>The container.</returns>
    private CompositionContainer CreateContainer()
    {
        try
        {
             var catalog = new AggregateCatalog();

             var directory = GetProviderDirectory();

             if (directory.Exists)
             {
                 catalog.Catalogs.Add(new DirectoryCatalog(directory.FullName));
             }

             var container = new CompositionContainer(catalog);
             container.ComposeParts(this);

             return container;
        }
        catch (Exception)
        {
            // TODO: Log Error
            throw;
        }
    }

    private static DirectoryInfo GetProviderDirectory()
    {
        var location = ConfigurationSupervisor.GetInstance().ProviderLocation;

        if (!string.IsNullOrWhiteSpace(location))
        {
            var providerLocation = new DirectoryInfo(Environment.ExpandEnvironmentVariables(location));

            if (providerLocation.Exists)
            {
                return providerLocation;
            }
        }

        // Use the current assembly location as the default.
        var exeLocation = new FileInfo(Assembly.GetExecutingAssembly().Location);

        return exeLocation.Directory;
    }
}

和插件:

[Export(typeof(IProviderModule))]
public class Provider : IProviderModule, IDisposable
{
    private IQueryEngine _queryEngine;

    #region Properties

    /// <summary>
    /// Gets or sets the query engine.
    /// </summary>
    [Import("QueryEngine", typeof(IQueryEngine), RequiredCreationPolicy = CreationPolicy.Shared)]
    public IQueryEngine QueryEngine
    {
        get { return _queryEngine; }
        set
        {
            _queryEngine = value;
        }

    }

    public void InvokeQueryEngine()
    {
        var item = _queryEngine.Get(); // Exception!
    }
}

要導出QueryEngine類型,您應該執行以下操作

[Export(typeof(IQueryEngine))]
public class QueryEngine : QueryEngine 

現在在插件類中導入它應該很簡單

[Import(typeof(IQueryEngine), RequiredCreationPolicy = CreationPolicy.Shared)]
public IQueryEngine QueryEngine
{
    get { return _queryEngine; }
    set
    {
        _queryEngine = value;
    }
}

我不太確定你在IQueryEngine字段的[Export]上嘗試做什么。 你應該能夠完全刪除它

暫無
暫無

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

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