{
public interface ILogger
{
void WriteLog(string message);
}
[Export(typeof(ILogger))]
public class DBLogger : ILogger
{
public void WriteLog(string message)
{
MessageBox.Show(message);
}
}
}
public ILogger Logger { get; set; }
{
//传统加载
[Import(typeof(ILogger))]
public ILogger Logger { get; set; }
//迟延加载
[Import]
public Lazy<ILogger> Service;
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
Logger.WriteLog("日志内容");
Service.Value.WriteLog("日志内容");
}
}
ThreadSafetyMode=PublicationOnly, IsValueCreated=true, IsValueFaulted=false, Value={MEFTraining.LzayImports.DBLogger}
IsValueCreated: true
Service.Value
{MEFTraining.LzayImports.DBLogger}
[MEFTraining.LzayImports.DBLogger]: {MEFTraining.LzayImports.DBLogger}
{
}
[Export(typeof(Users))]
public class Users
{
public string UserName = "张三";
}
{
[Import(typeof(Users))]
public Lazy<Users,IMetadata> Users { get; set; }
public MetadataControl()
{
InitializeComponent();
//宿主MEF托管扩展容器
CompositionInitializer.SatisfyImports(this);
MessageBox.Show(Users.Value.UserName);
}
}
ThreadSafetyMode=PublicationOnly, IsValueCreated=true, IsValueFaulted=false, Value={MEFTraining.LzayImports.Users}
base {System.Lazy<MEFTraining.LzayImports.Users>}: ThreadSafetyMode=PublicationOnly, IsValueCreated=true, IsValueFaulted=false, Value={MEFTraining.LzayImports.Users}
Metadata: {_proxy_MEFTraining.LzayImports.IMetadata_0174a468-9771-4271-a37e-9a4a83eca6bd}
{
string Name { get; }
}
[ExportMetadata("Name","李四")]
[Export(typeof(Users))]
public class Users
{
public string UserName = "张三";
}
{
[Import(typeof(Users))]
public Lazy<Users,IMetadata> Users { get; set; }
public MetadataControl()
{
InitializeComponent();
//宿主MEF托管扩展容器
CompositionInitializer.SatisfyImports(this);
MessageBox.Show(Users.Value.UserName);
}
}
{
public partial class MetadataControl : UserControl
{
[Import(typeof(Users))]
public Lazy<Users,IMetadata> Users { get; set; }
public MetadataControl()
{
InitializeComponent();
//宿主MEF托管扩展容器
CompositionInitializer.SatisfyImports(this);
MessageBox.Show(Users.Value.UserName);
}
}
public interface IMetadata
{
string Name { get; }
}
[ExportMetadata("Name","李四")]
[Export(typeof(Users))]
public class Users
{
public string UserName = "张三";
}
}