是否MEF(托管扩展框架)DO"鸭"打字?

是否MEF(托管扩展框架)DO"鸭"打字?

问题描述:

我有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代替。

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.

能否MEF做鸭打字对我来说,如果所有的接口点执行返回代理对象?

Can MEF do the "duck" typing for me and return a proxy object if all the interface points are implemented?

我认为这是有在MEF的早期版本(通过动态发射IL为班级和返回它),它现在是删除。这真的没有意义。毕竟,你的类应该的设计的实施该附加功能,通过一个特定的接口。如果你能像导出属性给他们,你应该是完全能够实现的类接口了。

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.