-
Notifications
You must be signed in to change notification settings - Fork 4k
/
MefHostServices.cs
147 lines (121 loc) · 5.2 KB
/
MefHostServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Composition.Hosting;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace Microsoft.CodeAnalysis.Host.Mef
{
public class MefHostServices : HostServices, IMefHostExportProvider
{
internal delegate MefHostServices CreationHook(IEnumerable<Assembly> assemblies);
/// <summary>
/// This delegate allows test code to override the behavior of <see cref="Create(IEnumerable{Assembly})"/>.
/// </summary>
/// <seealso cref="TestAccessor.HookServiceCreation"/>
private static CreationHook s_creationHook;
private readonly CompositionContext _compositionContext;
public MefHostServices(CompositionContext compositionContext)
=> _compositionContext = compositionContext;
public static MefHostServices Create(CompositionContext compositionContext)
{
if (compositionContext == null)
{
throw new ArgumentNullException(nameof(compositionContext));
}
return new MefHostServices(compositionContext);
}
public static MefHostServices Create(IEnumerable<System.Reflection.Assembly> assemblies)
{
if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}
if (s_creationHook != null)
{
return s_creationHook(assemblies);
}
var compositionConfiguration = new ContainerConfiguration().WithAssemblies(assemblies.Distinct());
var container = compositionConfiguration.CreateContainer();
return new MefHostServices(container);
}
protected internal override HostWorkspaceServices CreateWorkspaceServices(Workspace workspace)
=> new MefWorkspaceServices(this, workspace);
IEnumerable<Lazy<TExtension>> IMefHostExportProvider.GetExports<TExtension>()
=> _compositionContext.GetExports<TExtension>().Select(e => new Lazy<TExtension>(() => e));
IEnumerable<Lazy<TExtension, TMetadata>> IMefHostExportProvider.GetExports<TExtension, TMetadata>()
{
var importer = new WithMetadataImporter<TExtension, TMetadata>();
_compositionContext.SatisfyImports(importer);
return importer.Exports;
}
private class WithMetadataImporter<TExtension, TMetadata>
{
[ImportMany]
public IEnumerable<Lazy<TExtension, TMetadata>> Exports { get; set; }
}
#region Defaults
private static MefHostServices s_defaultHost;
public static MefHostServices DefaultHost
{
get
{
if (s_defaultHost == null)
{
var host = Create(DefaultAssemblies);
Interlocked.CompareExchange(ref s_defaultHost, host, null);
}
return s_defaultHost;
}
}
private static ImmutableArray<Assembly> s_defaultAssemblies;
public static ImmutableArray<Assembly> DefaultAssemblies
{
get
{
if (s_defaultAssemblies.IsDefault)
{
ImmutableInterlocked.InterlockedInitialize(ref s_defaultAssemblies, LoadDefaultAssemblies());
}
return s_defaultAssemblies;
}
}
// Used to build a MEF composition using the main workspaces assemblies and the known VisualBasic/CSharp workspace assemblies.
// updated: includes feature assemblies since they now have public API's.
private static readonly string[] s_defaultAssemblyNames = new string[]
{
"Microsoft.CodeAnalysis.Workspaces",
"Microsoft.CodeAnalysis.CSharp.Workspaces",
"Microsoft.CodeAnalysis.VisualBasic.Workspaces",
"Microsoft.CodeAnalysis.Features",
"Microsoft.CodeAnalysis.CSharp.Features",
"Microsoft.CodeAnalysis.VisualBasic.Features"
};
internal static bool IsDefaultAssembly(Assembly assembly)
{
var name = assembly.GetName().Name;
return s_defaultAssemblyNames.Contains(name);
}
private static ImmutableArray<Assembly> LoadDefaultAssemblies()
=> MefHostServicesHelpers.LoadNearbyAssemblies(s_defaultAssemblyNames);
#endregion
internal readonly struct TestAccessor
{
/// <summary>
/// Injects replacement behavior for the <see cref="Create(IEnumerable{Assembly})"/> method.
/// </summary>
internal static void HookServiceCreation(CreationHook hook)
{
s_creationHook = hook;
// The existing host, if any, is not retained past this call.
s_defaultHost = null;
}
}
}
}