6

我有 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 可以为我输入“鸭子”并返回代理对象吗?

4

2 回答 2

5

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

于 2009-07-17T00:36:54.803 回答
1

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

于 2012-04-14T20:20:03.953 回答