Search code examples
c#mefvsxvisual-studio-sdk

Visual Studio Extensibility - different MEF Export per Visual Studio version?


I'm authoring a Visual Studio extension and I want to [Export] a different implementation of a given interface based on which Visual Studio version is running - for instance, one implementation for Visual Studio 2010 and 2012 and a different one for Visual Studio 2013 and Visual Studio "14".

I realize I could simply deploy a different DLL for each Visual Studio version, but I'm wondering if there's a way to avoid that - shipping the same DLL and the same vsixmanifest, but having my extension dynamically [Export] the correct version of the interface.

What is the most eloquent way to do this?


Solution

  • It's not clear from your question that you actually need to have separate exports. Several options for supporting multiple versions of Visual Studio are available:

    1. Identify the minimum version of Visual Studio you wish to support, and only reference immutable and/or versioned assemblies from that version of Visual Studio and earlier. Your extension will likely work across multiple versions without changes or special considerations. This is the best option whenever "reasonably" possible.

    2. Export a single item, but implement it using assemblies that target specific versions of Visual Studio. For example, my Inheritance Margin extension requires a reference to an unversioned assembly, so I need to include separate implementations for each supported version of Visual Studio. It is implemented by providing a single common exported object CSharpInheritanceTaggerProvider, but the implementation of the tagger itself is delegated to a dynamically selected assembly.

    3. Export multiple items, but only perform operations within the one relevant to the current version of Visual Studio. The GitDiffMargin extension uses this feature to place the scroll margin control in the most appropriate location. While the assembly doesn't require any reference to unversioned assemblies, starting in Visual Studio 2013, the optimum location for this margin in the UI changed, and MEF metadata attributes statically determine the placement.