MEF(托管可扩展性框架)是否会“回避”? 打字?
我有 2 个程序集:
程序集 1:
interface IWeapon {
int Might { get; }
}
[Export("sword")]
public class Sword : IWeapon {
public int Might {
get { return 10; }
}
}
程序集 2:
interface IWeapon {
int Might { get; }
}
var catalog = new AssemblyCatalog(typeof(Ninja.Sword).Assembly);
var container = new CompositionContainer(catalog);
// not allowed to use the IWeapon def in assembly 2
var sword = container.GetExportedValue<IWeapon>("sword");
我知道如何让它工作。 我可以向 MEF(托管扩展性框架)询问该对象,或者让它导出正确的 IWeapon,而不仅仅是按名称导出对象。
如果实现了所有接口点,MEF 是否可以为我执行“鸭子”类型并返回代理对象?
I have 2 assemblies:
Assembly 1:
interface IWeapon {
int Might { get; }
}
[Export("sword")]
public class Sword : IWeapon {
public int Might {
get { return 10; }
}
}
Assembly 2:
interface IWeapon {
int Might { get; }
}
var catalog = new AssemblyCatalog(typeof(Ninja.Sword).Assembly);
var container = new CompositionContainer(catalog);
// not allowed to use the IWeapon def in assembly 2
var sword = container.GetExportedValue<IWeapon>("sword");
I know how to get this to work. I can either ask the MEF (Managed Extensibility Framework) for the object, or get it to export the correct IWeapon instead of just the object by name.
Can MEF do the "duck" typing for me and return a proxy object if all the interface points are implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为它存在于 MEF 的早期版本中(通过为类动态发出 IL 并返回它),但现在已被删除。 这确实没有道理。 毕竟,您的类应该设计以通过特定接口实现该加载项功能。 如果您可以向它们添加诸如
Export
属性之类的内容,那么您也应该能够完美地在您的类上实现该接口。I think it was there in early versions of MEF (by dynamically emitting IL for the class and returning it) and it's removed now. It really doesn't make sense. After all, your class should be designed to implement that add-in functionality through a specific interface. If you can add things like
Export
attribute to them, you should be perfectly able to implement the interface on your class too.如果您的两个 IWeapon 类具有相同的 COM Guid,那么您可以使用 .NET 4 中的类型等效来接近鸭子类型。这对于使用 MEF 插件的版本控制和升级支持非常好,即拥有一个 v2 合约,也可以加载以下插件只实现合约的 v1 版本。 这是一篇关于这个主题的好文章。
http://blogs.msdn.com/b/delay/archive/2011/03/09/mef -addict-combining-net-4-s-type-embedding-and-mef-to-enable-a-smooth-upgrade-story-for-applications-and-their-extensions.aspx
If both of your IWeapon classes have the same COM Guid then you can get close to duck typing using type equivalence in .NET 4. It's really nice for versioning and upgrade support of plugins with MEF i.e. Having a v2 contract that can also load plugins that only implement v1 of the contract. Here is a good article on the subject.
http://blogs.msdn.com/b/delay/archive/2011/03/09/mef-addict-combining-net-4-s-type-embedding-and-mef-to-enable-a-smooth-upgrade-story-for-applications-and-their-extensions.aspx