Search code examples
c#.netmef

Does MEF (Managed Extensibility Framework) do "duck" typing?


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?


Solution

  • 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.