2011-12-09 14 views
0

機能していないのImportMany:ExportFactoryの私は、次のような状況を実装しようとしています

インポート:

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

答えて

0

以前と同じように、質問を投稿した後すぐに答えを見つけました。私は本当に信じていますが、私は尋ねなかったが、私は今答えを持っていないだろう。

回答は、AttributeUsageAttributeのエクスポート属性を装飾していました。私はそれを次のように変更し、うまくいった。私はそれがInherited=falseビットと関係があると思います。

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

ありがとうございます! :)

デナダ!

関連する問題