Last active
November 7, 2024 13:10
-
-
Save madskristensen/4d205244dd92c37c82e7 to your computer and use it in GitHub Desktop.
Import MEF components from non-MEF exported classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.Composition; | |
using Microsoft.VisualStudio.Shell; | |
using Microsoft.VisualStudio.Shell.TableManager; | |
public class ExtensionPackage : Package | |
{ | |
[Import] | |
private ITableManagerProvider _tableManagerProvider; | |
protected override void Initialize() | |
{ | |
base.Initialize(); | |
this.SatisfyImportsOnce(); // This calls the extension method | |
ITableManager errorsTable = _tableManagerProvider.GetTableManager(StandardTables.ErrorsTable); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.Composition; | |
using Microsoft.VisualStudio.ComponentModelHost; | |
using Microsoft.VisualStudio.Shell; | |
public static class MefExtensions | |
{ | |
private static IComponentModel _compositionService; | |
public static void SatisfyImportsOnce(this object o) | |
{ | |
if (_compositionService == null) | |
{ | |
_compositionService = ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)) as IComponentModel; | |
} | |
if (_compositionService != null) | |
{ | |
_compositionService.DefaultCompositionService.SatisfyImportsOnce(o); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment