One way to do this is to write a factory for the logger yourself and use that as the contract you export.
public class Logger : ILogger
{
public Logger(IFoo foo) { }
// ...
}
[Export(typeof(ILoggerFactory))]
public class LoggerFactory : ILoggerFactory
{
[Import]
public IFoo Foo { get; set; }
public ILogger CreateLogger()
{
return new Logger(Foo);
}
}
Then you just import a LoggerFactory, and call CreateLogger every time you need a logger. This is pretty much the same thing you would do if you imported ExportFactory. The downside is that you have to write a separate factory for each thing you want to be able to create multiple instances of.
Another option is to add an ExportProvider to your container that allows you to import factories. In the latest MEF drop on CodePlex, there is a DynamicInstantiation sample which shows how to do this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…