-
Notifications
You must be signed in to change notification settings - Fork 4k
/
MefHostServices.cs
188 lines (157 loc) · 6.6 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
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, bool requestingDefaultHost);
/// <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)
{
var requestingDefaultAssemblies =
assemblies is ImmutableArray<Assembly> array
&& array == DefaultAssemblies;
return s_CreationHook(assemblies, requestingDefaultAssemblies);
}
var compositionConfiguration = new ContainerConfiguration().WithAssemblies(assemblies.Distinct());
var container = compositionConfiguration.CreateContainer();
return new MefHostServices(container);
}
protected internal override HostWorkspaceServices CreateWorkspaceServices(Workspace workspace)
{
return new MefWorkspaceServices(this, workspace);
}
IEnumerable<Lazy<TExtension>> IMefHostExportProvider.GetExports<TExtension>()
{
return _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 = MefHostServices.Create(MefHostServices.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;
}
}
private static ImmutableArray<Assembly> LoadDefaultAssemblies()
{
// 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.
var assemblyNames = new string[]
{
"Microsoft.CodeAnalysis.Workspaces",
"Microsoft.CodeAnalysis.CSharp.Workspaces",
"Microsoft.CodeAnalysis.VisualBasic.Workspaces",
"Microsoft.CodeAnalysis.Features",
"Microsoft.CodeAnalysis.CSharp.Features",
"Microsoft.CodeAnalysis.VisualBasic.Features"
};
return LoadNearbyAssemblies(assemblyNames);
}
internal static ImmutableArray<Assembly> LoadNearbyAssemblies(string[] assemblyNames)
{
var assemblies = new List<Assembly>();
foreach (var assemblyName in assemblyNames)
{
var assembly = TryLoadNearbyAssembly(assemblyName);
if (assembly != null)
{
assemblies.Add(assembly);
}
}
return assemblies.ToImmutableArray();
}
private static Assembly TryLoadNearbyAssembly(string assemblySimpleName)
{
var thisAssemblyName = typeof(MefHostServices).GetTypeInfo().Assembly.GetName();
var assemblyShortName = thisAssemblyName.Name;
var assemblyVersion = thisAssemblyName.Version;
var publicKeyToken = thisAssemblyName.GetPublicKeyToken().Aggregate(string.Empty, (s, b) => s + b.ToString("x2"));
if (string.IsNullOrEmpty(publicKeyToken))
{
publicKeyToken = "null";
}
var assemblyName = new AssemblyName(string.Format("{0}, Version={1}, Culture=neutral, PublicKeyToken={2}", assemblySimpleName, assemblyVersion, publicKeyToken));
try
{
return Assembly.Load(assemblyName);
}
catch (Exception)
{
return null;
}
}
#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;
}
}
}
}