问题
I am wanting to use mef with the Logging and Exception Blocks of Enterprise Library 5.0.
What I am doing is I have a plugin defined that MEF is looking for in a directory. It then imports it for me. However I am using logging and exception handling in the plugin also and I would like to be able to insert the instance of the LogWriter and ExceptionWriter via mef. The example shows you how to do it with unity which won't work since I am using mef to get the plugin and instantiate it.
thanks
回答1:
You can export LogWriter
with a class like this:
public class LogWriterExporter
{
[Export(typeof(LogWriter))]
public LogWriter LogWriter
{
get
{
return new LogWriter(...);
}
}
}
Note that MEF will typically call this property getter only once, except if an importer demands CreationPolicy.NonShared
. If you want to enforce singleton behavior, then you can explicitly add a [PartCreationPolicy(CreationPolicy.Shared)]
attribute to the class.
来源:https://stackoverflow.com/questions/5637678/net-mef-with-enterprise-library-5-0