In the MEF Programming Guide , read the Export and Metadata section. It shows how you can add metadata to the exported part using the ExportMetadata attribute or defining your own custom export attribute.
You can then define the ILoggerMetadata interface as follows:
public interface ILoggerMetadata { string Catagory { get; } }
and execute ImportMany a IEnumerable<Lazy<ILogger,ILoggerMetadata>> and select the one you want in the code, for example:
private ILogger fooLogger; [ImportMany] public IEnumerable<Lazy<ILogger,ILoggerMetadata>> Loggers { set { this.fooLogger = value.First(x => x.Metadata.Catagory == "foo").Value; } }
I agree that it would be better to put metadata restrictions directly in the import attribute, but this is currently not possible in the MEF out of the box. (It may be possible to extend the MEF for this.)
Another approach is to output the IFooLogger interface from ILogger and use this in your import and export. It is simple and has the same effect as import restrictions. However, this approach does not work if you have several metadata attributes and / or many possible values.
edit: I subtly misunderstood your question; I thought that this was due to the restriction of imports, and not with the configuration of the imported object with some additional parameters.
I think this recent article from Kathleen Dollard addresses the same issue. Also, in this post about component relationships , Nicholas Bloomhardt models such a โparameterizationโ as an injection of Func<X,Y> (or Func<ILogger,string> in your case).
You can do the same in MEF by placing the [Export(typeof(Func<ILogger,string>))] attribute directly on the method. Or, if you need a less ambiguous contract, you can define an ILoggerFactory interface and import / export, which:
public ILoggerFactory { ILogger Create(string category); }
In the end, you still have to call factory in the code.