MEF(托管可扩展性框架)是否"躲避"打字?

Sam*_*ron 6 .net c# mef

我有2个组件:

大会1:

interface IWeapon {
    int Might { get; }
}

[Export("sword")]
public class Sword : IWeapon {

    public int Might {
        get { return 10; }
    }
}
Run Code Online (Sandbox Code Playgroud)

大会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");
Run Code Online (Sandbox Code Playgroud)

我知道如何让它发挥作用.我可以向MEF(Managed Extensibility Framework)询问该对象,或者让它导出正确的IWeapon而不是按名称导出对象.

如果所有接口点都已实现,MEF可以为我做"鸭子"打字并返回代理对象吗?

Meh*_*ari 5

我认为在早期版本的MEF中存在(通过为类动态发送IL并返回它)并且它现在已被删除.这真的没有意义.毕竟,您的类应该设计为通过特定接口实现该加载项功能.如果你可以Export为它们添加属性之类的东西,你也应该完全能够在你的类上实现接口.