ImportMany的ImportMactory不起作用

时间:2011-12-09 18:27:53

标签: silverlight mef

我正在尝试实施以下情况:

导入:

private IEnumerable<ExportFactory<IPage, IPageMetadata>> _pageFactories = null;

[ImportMany("Page", typeof(IPage), AllowRecomposition = true)]
public IEnumerable<ExportFactory<IPage, IPageMetadata>> PageFactories
{
    get { return _pageFactories; }
    set { _pageFactories = value; }
}

导出:

[PartCreationPolicy(CreationPolicy.NonShared)]
[ExportPage(ModuleNames.Kiosk, "/Kiosk/CreateProject", typeof(IPage))]
public partial class ProjectView : IPage
{

}

导出属性:

public interface IPageMetadata
{
    String ModuleName { get; }
    String Version { get; }
    String RelativeUri { get; }
}

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public class ExportPageAttribute : ExportAttribute
{
    private String _strModuleName = "";
    private String _strVersion = "0.0";

    private String _strRelativeUri = "";

    public ExportPageAttribute(String a_strModuleName, String a_strRelativeUri)
        : base("Page")
    {
        _strRelativeUri = a_strRelativeUri;
        _strModuleName = a_strModuleName;
    }

    public ExportPageAttribute(String a_strModuleName, String a_strRelativeUri, Type a_contractType)
        : base("Page", a_contractType)
    {
        _strRelativeUri = a_strRelativeUri;
        _strModuleName = a_strModuleName;
    }

    public String RelativeUri
    {
        get { return _strRelativeUri; }
        private set { _strRelativeUri = value;  }
    }

    public String ModuleName
    {
        get { return _strModuleName; }
        private set { _strModuleName = value; }
    }

    public String Version
    {
        get { return _strVersion; }
        set { _strVersion = value; }
    }
}

容器创建:

 AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
 AggregateCatalog aggregateCatalog = new AggregateCatalog(assemblyCatalog);

 CompositionContainer compositionContainer = new CompositionContainer(aggregateCatalog);

 CompositionHost.Initialize(compositionContainer);

当调用PageFactories_set时(它是),提供的序列为空。以下确实有效:

    private IPage[] _pages;

    [ImportMany("Page", typeof(IPage), AllowRecomposition = true)]
    public IPage[] Pages
    {
        get { return _pages; }
        set { _pages = value; }
    }

1 个答案:

答案 0 :(得分:0)

就像以前那么多次,我在发布问题后很快就找到了答案。我确实相信,如果我没有问,我现在没有答案。

答案是AttributeUsageAttribute装饰我的导出属性。我将其更改为以下内容,并且有效。我认为它与Inherited=false位有关。

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]

谢谢! :)

De nada!