tags:

views:

255

answers:

1

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?

+4  A: 

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.

Mehrdad Afshari
The problem I see is versioning, if your base assembly changes versions, all the extensions may need recompilation.
Sam Saffron
I'd solve that issue by moving the interface to a separate assembly. The version of that assembly should change only if the actual contract changes.
Mehrdad Afshari
Yeah Ive thought of that, its still a tad annoying cause now I will have to keep track of 2 different assemblies (deploy 2 assemblies etc), I guess it is the proper way of doing things with .Net
Sam Saffron
It actually makes sense since the *contract version* is different from *consumer version* and you will be free to choose to break or ensure compatibility in your future versions of the main assembly or you could provide two different versions of the contract for old addins and newer addins.
Mehrdad Afshari
Designing dynamic systems is inherently more complex and requires things such as splitting the contract assembly. If you don't want that hassle then maybe a dynamic system isn't for you.
Andrew Burns
Accepting this cause, the answer is basically no, it dont exist in mef no more
Sam Saffron