I am trying to implement the following situation:
Import:
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; }
}
Export:
[PartCreationPolicy(CreationPolicy.NonShared)]
[ExportPage(ModuleNames.Kiosk, "/Kiosk/CreateProject", typeof(IPage))]
public partial class ProjectView : IPage
{
}
Export Attribute:
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; }
}
}
Container Creation:
AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
AggregateCatalog aggregateCatalog = new AggregateCatalog(assemblyCatalog);
CompositionContainer compositionContainer = new CompositionContainer(aggregateCatalog);
CompositionHost.Initialize(compositionContainer);
When the PageFactories_set is called (which it is) the provided sequence is empty. The following does work though:
private IPage[] _pages;
[ImportMany("Page", typeof(IPage), AllowRecomposition = true)]
public IPage[] Pages
{
get { return _pages; }
set { _pages = value; }
}
Like so many times before, I figured out the answer soon after posting the question. I really do believe though that, had I not asked, I’d not have an answer now.
The answer was with the
AttributeUsageAttributedecorating my export attribute. I change it to the following, and it worked. I think it has something to do with theInherited=falsebit.Thanks me! 🙂
De nada!