forked from Azure/azure-sdk-tools
/
ReflectionService.cs
286 lines (248 loc) · 13.1 KB
/
ReflectionService.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
namespace MS.Az.Mgmt.CI.Common.Services
{
using Microsoft.Build.Framework;
using MS.Az.Mgmt.CI.BuildTasks.Common.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
public class ReflectionService : NetSdkUtilTask
{
#region fields
Assembly _asmToReflect;
MetadataLoadContext _mlc;
#endregion
#region Properties
Assembly AssemblyToReflect
{
get
{
if(_asmToReflect == null)
{
_asmToReflect = GetAssembly(UseMetadataLoadContext);
}
return _asmToReflect;
}
set
{
_asmToReflect = value;
}
}
MetadataLoadContext MetaCtx
{
get
{
if (_mlc == null)
{
_mlc = new MetadataLoadContext(new SimpleAssemblyResolver());
}
return _mlc;
}
}
public string AssemblyToReflectFilePath { get; set; }
public bool UseMetadataLoadContext { get; set; }
#endregion
#region Constructor
public ReflectionService()
{
UtilLogger.LogInfo(MessageImportance.Low, MetaCtx.ToString());
}
public ReflectionService(string AssemblyFilePath) : this(AssemblyFilePath, useMetadataLoadContext: true) { }
public ReflectionService(string assemblyFilePath, bool useMetadataLoadContext = true)
{
AssemblyToReflectFilePath = assemblyFilePath;
UseMetadataLoadContext = useMetadataLoadContext;
}
#endregion
#region Public Functions
public Assembly GetAssembly(bool useMetadataLoadContext)
{
Assembly loadedAssemly = null;
if (useMetadataLoadContext)
{
if(!string.IsNullOrWhiteSpace(AssemblyToReflectFilePath))
{
loadedAssemly = MetaCtx.LoadFromAssemblyPath(AssemblyToReflectFilePath);
}
else
{
UtilLogger.LogInfo(MetaCtx.ToString());
}
}
else
{
loadedAssemly = Assembly.LoadFrom(AssemblyToReflectFilePath);
}
return loadedAssemly;
}
public List<PropertyInfo> GetProperties(string typeNameStartWith, string propertyName)
{
List<PropertyInfo> propertyList = new List<PropertyInfo>();
try
{
var exportedNS = GetAssembly(true).ExportedTypes.Select<Type, string>((item) => item.Namespace);
var distinctNS = exportedNS.Distinct<string>();
Type sdkInfoType = null;
foreach(string ns in distinctNS)
{
string fqtype = string.Format("{0}.sdkinfo", ns);
sdkInfoType = GetAssembly(false).GetType(fqtype, throwOnError: true, ignoreCase: true);
if(sdkInfoType != null)
{
break;
}
}
UtilLogger.LogInfo("Querying Type '{0}' for propertyName '{1}'", sdkInfoType.Name, propertyName);
PropertyInfo[] memInfos = sdkInfoType.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
foreach (PropertyInfo mInfo in memInfos)
{
UtilLogger.LogInfo("Found:'{0}'", mInfo.Name);
if (mInfo.Name.Contains(propertyName))
{
UtilLogger.LogInfo("Added:'{0}' to list", mInfo.Name);
propertyList.Add(mInfo);
}
}
}
catch (Exception ex)
{
UtilLogger.LogWarning(ex.Message);
}
return propertyList;
}
#endregion
public override void Dispose()
{
AssemblyToReflect = null;
IsDisposed = true;
}
}
class SimpleAssemblyResolver : CoreMetadataAssemblyResolver
{
private static readonly Version s_Version0000 = new Version(0, 0, 0, 0);
public SimpleAssemblyResolver() { }
public override Assembly Resolve(MetadataLoadContext context, AssemblyName assemblyName)
{
Assembly core = base.Resolve(context, assemblyName);
if (core != null)
return core;
ReadOnlySpan<byte> pktFromAssemblyName = assemblyName.GetPublicKeyToken();
foreach (Assembly assembly in context.GetAssemblies())
{
AssemblyName assemblyNameFromContext = assembly.GetName();
if (assemblyName.Name.Equals(assemblyNameFromContext.Name, StringComparison.OrdinalIgnoreCase) &&
NormalizeVersion(assemblyName.Version).Equals(assemblyNameFromContext.Version) &&
pktFromAssemblyName.SequenceEqual(assemblyNameFromContext.GetPublicKeyToken()) &&
NormalizeCultureName(assemblyName.CultureName).Equals(NormalizeCultureName(assemblyNameFromContext.CultureName)))
return assembly;
}
return null;
}
private Version NormalizeVersion(Version version)
{
if (version == null)
return s_Version0000;
return version;
}
private string NormalizeCultureName(string cultureName)
{
if (cultureName == null)
return string.Empty;
return cultureName;
}
}
class CoreMetadataAssemblyResolver : MetadataAssemblyResolver
{
#region fields
private Assembly _coreAssembly;
#endregion
#region Constructor
public CoreMetadataAssemblyResolver() { }
#endregion
#region Public Functions
public override Assembly Resolve(MetadataLoadContext context, AssemblyName assemblyName)
{
string name = assemblyName.Name;
if (name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase) ||
name.Equals("System.Private.CoreLib", StringComparison.OrdinalIgnoreCase) ||
name.Equals("System.Runtime", StringComparison.OrdinalIgnoreCase) ||
name.Equals("netstandard", StringComparison.OrdinalIgnoreCase) ||
name.Equals("System.Reflection.Metadata", StringComparison.OrdinalIgnoreCase) ||
// For interop attributes such as DllImport and Guid:
name.Equals("System.Runtime.InteropServices", StringComparison.OrdinalIgnoreCase))
{
if (_coreAssembly == null)
//_coreAssembly = context.LoadFromByteArray(s_SimpleNameOnlyImage);
_coreAssembly = context.LoadFromStream(CreateStreamForCoreAssembly());
return _coreAssembly;
}
return null;
}
#endregion
#region private functions
Stream CreateStreamForCoreAssembly()
{
// We need a core assembly in IL form. Since this version of this code is for Jitted platforms, the System.Private.Corelib
// of the underlying runtime will do just fine.
string assumedLocationOfCoreLibrary = typeof(object).Assembly.Location;
if (assumedLocationOfCoreLibrary == null || assumedLocationOfCoreLibrary == string.Empty)
{
throw new Exception("Could not find a core assembly to use for tests as 'typeof(object).Assembly.Location` returned " +
"a null or empty value. The most likely cause is that you built the tests for a Jitted runtime but are running them " +
"on an AoT runtime.");
}
return File.OpenRead(GetPathToCoreAssembly());
}
string GetPathToCoreAssembly()
{
return typeof(object).Assembly.Location;
}
string GetNameOfCoreAssembly()
{
return typeof(object).Assembly.GetName().Name;
}
#endregion
#region assembly
public static readonly byte[] s_SimpleNameOnlyImage = Convert.FromBase64String(
"TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFt" +
"IGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAFlyX1sAAAAAAAAAAOAAIiALATAAAAQAAAAGAAAAAAAA8iIAAAAgAAAAQAAA" +
"AAAAEAAgAAAAAgAABAAAAAAAAAAEAAAAAAAAAACAAAAAAgAAAAAAAAMAQIUAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAAKAiAABPAAAAAEAAAMAC" +
"AAAAAAAAAAAAAAAAAAAAAAAAAGAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAAAAAAAAAAAAAAA" +
"CCAAAEgAAAAAAAAAAAAAAC50ZXh0AAAA+AIAAAAgAAAABAAAAAIAAAAAAAAAAAAAAAAAACAAAGAucnNyYwAAAM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