/
ResolverExtensions.cs
248 lines (216 loc) · 8.55 KB
/
ResolverExtensions.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition.Reflection
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
public static class ResolverExtensions
{
private const BindingFlags AllInstanceMembers = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private const BindingFlags AllMembers = AllInstanceMembers | BindingFlags.Static;
public static Type Resolve(this TypeRef typeRef)
{
return typeRef?.ResolvedType;
}
public static MethodBase Resolve(this MethodRef methodRef)
{
if (methodRef == null)
{
return null;
}
MethodBase method = null;
#if RuntimeHandles
if (TryUseFastReflection(methodRef.DeclaringType, out Module manifest))
{
method = manifest.ResolveMethod(methodRef.MetadataToken);
}
else
#endif
{
TypeInfo declaringType = methodRef.DeclaringType.ResolvedType.GetTypeInfo();
var candidates = methodRef.Name == ConstructorInfo.ConstructorName
? (MethodBase[])declaringType.GetConstructors(AllInstanceMembers)
: declaringType.GetMethods(AllMembers);
method = FindMethodByParameters(candidates, methodRef.Name, methodRef.ParameterTypes);
}
if (methodRef.GenericMethodArguments.Length > 0)
{
var constructedMethod = ((MethodInfo)method).MakeGenericMethod(methodRef.GenericMethodArguments.Select(Resolve).ToArray());
return constructedMethod;
}
return method;
}
public static PropertyInfo Resolve(this PropertyRef propertyRef)
{
if (propertyRef == null)
{
return null;
}
Type type = propertyRef.DeclaringType.ResolvedType;
if (TryUseFastReflection(propertyRef.DeclaringType, out Module manifest))
{
return type.GetRuntimeProperties().First(p => p.MetadataToken == propertyRef.MetadataToken);
}
else
{
return type.GetProperty(propertyRef.Name, AllMembers);
}
}
public static MethodInfo ResolveGetter(this PropertyRef propertyRef)
{
if (propertyRef.GetMethodMetadataToken.HasValue)
{
#if RuntimeHandles
if (TryUseFastReflection(propertyRef.DeclaringType, out Module manifest))
{
return (MethodInfo)manifest.ResolveMethod(propertyRef.GetMethodMetadataToken.Value);
}
else
#endif
{
return propertyRef.PropertyInfo.GetMethod;
}
}
return null;
}
public static MethodInfo ResolveSetter(this PropertyRef propertyRef)
{
if (propertyRef.SetMethodMetadataToken.HasValue)
{
#if RuntimeHandles
if (TryUseFastReflection(propertyRef.DeclaringType, out Module manifest))
{
return (MethodInfo)manifest.ResolveMethod(propertyRef.SetMethodMetadataToken.Value);
}
else
#endif
{
return propertyRef.PropertyInfo.SetMethod;
}
}
return null;
}
public static FieldInfo Resolve(this FieldRef fieldRef)
{
if (fieldRef == null)
{
return null;
}
#if RuntimeHandles
if (TryUseFastReflection(fieldRef.DeclaringType, out Module manifest))
{
return manifest.ResolveField(fieldRef.MetadataToken);
}
else
#endif
{
return Resolve(fieldRef.DeclaringType).GetField(fieldRef.Name, AllMembers);
}
}
public static ParameterInfo Resolve(this ParameterRef parameterRef)
{
if (parameterRef == null)
{
return null;
}
MethodBase method;
#if RuntimeHandles
if (TryUseFastReflection(parameterRef.DeclaringType, out Module manifest))
{
method = manifest.ResolveMethod(parameterRef.Method.MetadataToken);
}
else
#endif
{
method = parameterRef.Method.MethodBase;
}
return method.GetParameters()[parameterRef.ParameterIndex];
}
internal static void GetInputAssemblies(this TypeRef typeRef, ISet<AssemblyName> assemblies)
{
Requires.NotNull(assemblies, nameof(assemblies));
if (typeRef != null)
{
assemblies.Add(typeRef.AssemblyName);
foreach (var typeArg in typeRef.GenericTypeArguments)
{
GetInputAssemblies(typeArg, assemblies);
}
assemblies.Add(typeRef.AssemblyName);
// Base types may define [InheritedExport] attributes or otherwise influence MEF
// so we should include them as input assemblies.
if (!typeRef.IsShallow)
{
foreach (var baseType in typeRef.BaseTypes)
{
assemblies.Add(baseType.AssemblyName);
}
}
}
}
internal static Module GetManifest(this Resolver resolver, AssemblyName assemblyName)
{
return resolver.AssemblyLoader.LoadAssembly(assemblyName).ManifestModule;
}
/// <summary>
/// Tests whether we can safely use fast reflection on the assembly that defines the given type.
/// </summary>
/// <param name="typeRef">The reference to a type that needs to be reflected over.</param>
/// <param name="manifest">Receives the manifest of the assembly that defines the type. May be <c>null</c> when fast reflection must not be used.</param>
/// <returns><c>true</c> if it is safe to use fast reflection; <c>false</c> otherwise.</returns>
internal static bool TryUseFastReflection(TypeRef typeRef, out Module manifest)
{
#if RuntimeHandles
manifest = typeRef.Resolver.GetManifest(typeRef.AssemblyName);
return IsStrongAssemblyIdentityMatch(typeRef, manifest);
#else
manifest = null;
return false;
#endif
}
private static T FindMethodByParameters<T>(IEnumerable<T> members, string memberName, ImmutableArray<TypeRef> parameterTypes)
where T : MethodBase
{
Requires.NotNull(members, nameof(members));
foreach (var member in members)
{
if (member.Name != memberName)
{
continue;
}
var parameters = member.GetParameters();
if (parameters.Length != parameterTypes.Length)
{
continue;
}
for (int i = 0; i < parameters.Length; i++)
{
if (!parameterTypes[i].Equals(parameters[i].ParameterType))
{
continue;
}
}
return member;
}
return default(T);
}
/// <summary>
/// Determines whether the metadata tokens stored in a <see cref="TypeRef"/>
/// can be considered reliable considering the currently loaded assembly manifest.
/// </summary>
/// <param name="typeRef">The <see cref="TypeRef"/> that may have been cached, possibly against a different build of the assembly.</param>
/// <param name="manifest">The manifest from the assembly that defines the referenced type.</param>
/// <returns><c>true</c> if the currently loaded assembly is the same build as the one that was cached.</returns>
private static bool IsStrongAssemblyIdentityMatch(TypeRef typeRef, Module manifest)
{
Requires.NotNull(typeRef, nameof(typeRef));
Requires.NotNull(manifest, nameof(manifest));
return typeRef.Resolver.GetStrongAssemblyIdentity(manifest.Assembly, typeRef.AssemblyName).Equals(typeRef.AssemblyId);
}
}
}