12
OK I'm pretty sure this is something blindingly obvious but I'm not finding it.
I'm trying to export a object from MEF container based on it's Metadata.
I've seen this done in tutorials like this one:
However my export doesn't have the Metadata property that is necessary for this to work. What could be the problem there?
[Export(typeof(IController))]
[ExportMetadata("controllerName","Home")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class HomeController : Controller{}
and again
public class MyControllerFactory : IControllerFactory
{
private readonly CompositionContainer _container;
public MyControllerFactory(CompositionContainer container)
{
_container = container;
}
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
var controllerExport = _container.GetExports<IController>().
Where(exp => exp.Metadata) //Here it doesn't have the Metadata property.
}
}
I understand that the GetExports returns a collection of Lazy that do of course not have the Metadata property but it's assumed in most tutorials I look through.
How do I do this correctly?
EDIT
This is what I did:
public interface IControllerMetaData
{
string Name { get; }
string Subdomain { get; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class ControllerMetadataAttribute : ExportAttribute
{
public ControllerMetadataAttribute(string name, string subdomain)
: base(typeof(IControllerMetaData))