理解 MEF 容器部件生命周期和实现是非常重要的事情。考虑到 MEF 关注可扩展应用程序。
[PartCreationPolicy(CreationPolicy.NonShared)] [Export(typeof(IMessageSender))] public class SmtpSender : IMessageSender { public void Send(string message) { throw new NotImplementedException(); } } public interface IMessageSender { void Send(string message); }
[Export] public class Importer { [Import(RequiredCreationPolicy = CreationPolicy.NonShared)] public Dependency Dep { get; set; } }
- | Part.Any | Part.Shared | Part.NonShared |
---|---|---|---|
Import.Any | Shared | Shared | Non Shared |
Import.Shared | Shared | Shared | No Match |
Import.NonShared | Non Shared | No Match | Non Shared |
MEF 编程指南(九):部件生命周期,布布扣,bubuko.com
原文:http://www.cnblogs.com/JavCof/p/3689129.html