how to inject a logger instance using MEF?
Is it possible to inject an instance of logger class object using Manage开发者_如何学Pythond Extensibility Framework? Please show a sample code snippet.
Yes, using Constructor Injection:
private readonly ILogger logger;
[ImportingConstructor]
public MyClass(ILogger logger)
{
this.logger = logger;
}
Obviously (if you know MEF) some other type must Export ILogger
...
Alternatively, you can also use Property Injection, although I find the semantics vague:
[Import(typeof(ILogger))]
public ILogger Logger { get; set; }
精彩评论