Does the MEF (Managed Extensibility Framework) use duck printing?

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 make this work. I can either ask the MEF (Managed Extensibility Framework) for the object, or get it to export the correct IWeapon instead of the name of the object by name.

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

+6
c # mef
source share
2 answers

I think it was there in early versions of MEF (by dynamically emitting IL for the class and returning it), and now it has been removed. It really doesn't make sense. In the end, your class must be designed to implement this add-in functionality through a specific interface. If you can add attributes like Export , you can also implement the interface in your class.

+5
source share

If both of your IWeapon classes have the same COM pointer, you can approach the duck set using type equivalence in .NET 4. This is really good for versioning and updating plugins with MEF, i.e. having a v2 contract that can also load plugins that only implement the v1 contract. Here is a good article on this.

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

+1
source share

All Articles