-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyResolver.cs
More file actions
468 lines (410 loc) · 17.1 KB
/
AssemblyResolver.cs
File metadata and controls
468 lines (410 loc) · 17.1 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#region Licence...
//-----------------------------------------------------------------------------
// Date: 17/10/04 Time: 2:33p
// Module: AssemblyResolver.cs
// Classes: AssemblyResolver
//
// This module contains the definition of the AssemblyResolver class. Which implements
// some methods for simplified Assembly navigation
//
// Written by Oleg Shilo (oshilo@gmail.com)
//----------------------------------------------
// The MIT License (MIT)
// Copyright (c) 2014 Oleg Shilo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------
#endregion Licence...
using System;
using System.Reflection;
#if net1
using System.Collections;
#else
using System.Collections.Generic;
#endif
using System.IO;
using csscript;
////////////////////////////////////////////////////
//
// shfusion.dll is no longer supported by .NET 4.0
//
////////////////////////////////////////////////////
namespace CSScriptLibrary
{
class MyClass
{
public MyClass()
{
}
}
/// <summary>
/// Class for resolving assembly name to assembly file
/// </summary>
public class AssemblyResolver
{
#region Class public data...
/// <summary>
/// File to be excluded from assembly search
/// </summary>
static public string ignoreFileName = "";
#endregion Class public data...
#region Class public methods...
static bool cacheProbingResults;
/// <summary>
/// Gets or sets a value indicating whether the assembly probing results should be cached. Default value is <c>false</c>;
/// <para>
/// Caching means that during the probing if the assembly is not found in one of the probing directories this directory will not
/// be checked again if the same assembly is to be resolved in the future.
/// </para>
/// <para>
/// This setting is to be used with the caution. While it can bring some performance benefits when the list of probing directories
/// is large it also may be wrong to assume that if the assembly in not found in a particular directory it still will not be there if the probing is repeated.
/// </para>
/// </summary>
/// <value><c>true</c> if probing results should be cached; otherwise, <c>false</c>.</value>
public static bool CacheProbingResults
{
get
{
return cacheProbingResults;
}
set
{
cacheProbingResults = value;
if (!value)
lock (NotFoundAssemblies)
{
NotFoundAssemblies.Clear();
}
}
}
#if net1
private static readonly System.Collections.Hashtable NotFoundAssemblies = new System.Collections.Hashtable();
#else
private static readonly HashSet<int> NotFoundAssemblies = new HashSet<int>();
#endif
private static int BuildHashSetValue(string assemblyName, string directory)
{
return CSSUtils.GetHashCodeEx((assemblyName ?? "") + (directory ?? ""));
}
//private static Assembly TryLoadAssemblyFrom(string assemblyName, string asmFile)
//{
// try
// {
// AssemblyName asmName = AssemblyName.GetAssemblyName(asmFile);
// if (asmName != null && asmName.FullName == assemblyName)
// return Assembly.LoadFrom(asmFile);
// else if (assemblyName.IndexOf(",") == -1 && asmName.FullName.StartsWith(assemblyName)) //short name requested
// return Assembly.LoadFrom(asmFile);
// }
// catch { }
// return null;
//}
#if net4
private static Assembly LoadAssemblyFrom(string assemblyName, string asmFile, bool throwException = false)
#else
private static Assembly LoadAssemblyFrom(string assemblyName, string asmFile, bool throwException)
#endif
{
try
{
if (asmFile.EndsWith(".cs"))
return null;
AssemblyName asmName = AssemblyName.GetAssemblyName(asmFile);
if (asmName != null && asmName.FullName == assemblyName)
return Assembly.LoadFrom(asmFile);
else if (assemblyName.IndexOf(",") == -1 && asmName.FullName.StartsWith(assemblyName)) //short name requested
return Assembly.LoadFrom(asmFile);
}
catch
{
if (throwException)
throw;
}
return null;
}
/// <summary>
/// Resolves assembly name to assembly file. Loads assembly file to the current AppDomain.
/// </summary>
/// <param name="assemblyName">The name of assembly</param>
/// <param name="dir">The name of directory where local assemblies are expected to be</param>
/// <param name="throwExceptions">if set to <c>true</c> [throw exceptions].</param>
/// <returns>loaded assembly</returns>
#if net4
static public Assembly ResolveAssembly(string assemblyName, string dir, bool throwExceptions = false)
#else
static public Assembly ResolveAssembly(string assemblyName, string dir, bool throwExceptions)
#endif
{
if (dir == null)
return null;
int hashSetValue = -1;
if (CacheProbingResults)
{
hashSetValue = BuildHashSetValue(assemblyName, dir);
lock (NotFoundAssemblies)
{
if (NotFoundAssemblies.Contains(hashSetValue))
return null;
}
}
try
{
if (Directory.Exists(dir))
{
Assembly retval = null;
string[] asmFileNameTokens = assemblyName.Split(",".ToCharArray(), 5);
string asmFile = Path.Combine(dir, asmFileNameTokens[0]);
if (ignoreFileName != Path.GetFileName(asmFile) && File.Exists(asmFile))
if (null != (retval = LoadAssemblyFrom(assemblyName, asmFile, throwExceptions)))
return retval;
//try file with name AssemblyDisplayName + .dll
asmFile = Path.Combine(dir, asmFileNameTokens[0]) + ".dll";
if (ignoreFileName != Path.GetFileName(asmFile) && File.Exists(asmFile))
if (null != (retval = LoadAssemblyFrom(assemblyName, asmFile, throwExceptions)))
return retval;
//try file with extension
foreach (string file in Directory.GetFiles(dir, asmFileNameTokens[0] + "*"))
if (null != (retval = LoadAssemblyFrom(assemblyName, file, throwExceptions)))
return retval;
}
}
catch
{
if (throwExceptions)
throw;
}
if (CacheProbingResults)
{
lock (NotFoundAssemblies)
{
#if net1
NotFoundAssemblies.Add(hashSetValue, null);
#else
NotFoundAssemblies.Add(hashSetValue);
#endif
}
}
return null;
}
static readonly char[] illegalChars = ":*?<>|\"".ToCharArray();
/// <summary>
/// Determines whether the string is a legal path token.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>
/// <c>true</c> if the string is a legal path token; otherwise, <c>false</c>.
/// </returns>
static public bool IsLegalPathToken(string name)
{
return name.IndexOfAny(illegalChars) != -1;
}
/// <summary>
/// Resolves namespace/assembly(file) name into array of assembly locations (local and GAC ones).
/// </summary>
/// <param name="name">'namespace'/assembly(file) name</param>
/// <param name="searchDirs">Assembly search directories</param>
/// <returns>collection of assembly file names where namespace is implemented</returns>
static public string[] FindAssembly(string name, string[] searchDirs)
{
#if net1
ArrayList retval = new ArrayList();
#else
List<string> retval = new List<string>();
#endif
if (!IsLegalPathToken(name))
{
foreach (string dir in searchDirs)
{
foreach (string asmLocation in FindLocalAssembly(name, dir)) //local assemblies alternative locations
retval.Add(asmLocation);
if (retval.Count != 0)
break;
}
if (retval.Count == 0)
{
string nameSpace = Utils.RemoveAssemblyExtension(name);
foreach (string asmGACLocation in FindGlobalAssembly(nameSpace))
{
retval.Add(asmGACLocation);
}
}
}
else
{
try
{
if (Path.IsPathRooted(name) && File.Exists(name)) //note relative path will return IsLegalPathToken(name)==true
retval.Add(name);
}
catch { } //does not matter why...
}
#if net1
return (string[])retval.ToArray(typeof(string));
#else
return retval.ToArray();
#endif
}
/// <summary>
/// Resolves namespace into array of local assembly locations.
/// (Currently it returns only one assembly location but in future
/// it can be extended to collect all assemblies with the same namespace)
/// </summary>
/// <param name="name">namespace/assembly name</param>
/// <param name="dir">directory</param>
/// <returns>collection of assembly file names where namespace is implemented</returns>
static public string[] FindLocalAssembly(string name, string dir)
{
//We are returning and array because name may represent assembly name or namespace
//and as such can consist of more than one assembly file (multiple assembly file is not supported at this stage).
try
{
string asmFile = Path.Combine(dir, name);
//cannot just check Directory.Exists(dir) as "name" can contain sum subDir parts
if (Directory.Exists(Path.GetDirectoryName(asmFile)))
{
//test well-known assembly extensions first
foreach (string ext in new string[] { "", ".dll", ".exe", ".compiled" })
{
string file = asmFile + ext; //just in case if user did not specify the extension
if (ignoreFileName != Path.GetFileName(file) && File.Exists(file))
return new string[] { file };
}
if (asmFile != Path.GetFileName(asmFile) && File.Exists(asmFile))
return new string[] { asmFile };
}
}
catch { } //name may not be a valid path name
return new string[0];
}
/// <summary>
/// Resolves namespace into array of global assembly (GAC) locations.
/// </summary>
/// <param name="namespaceStr">'namespace' name</param>
/// <returns>collection of assembly file names where namespace is implemented</returns>
static public string[] FindGlobalAssembly(String namespaceStr)
{
#if net1
ArrayList retval = new ArrayList();
#else
List<string> retval = new List<string>();
#endif
try
{
AssemblyEnum asmEnum = new csscript.AssemblyEnum(namespaceStr);
string highestVersion = "";
string asmName = "";
do
{
asmName = asmEnum.GetNextAssembly();
if (string.Compare(asmName, highestVersion) > 0)
highestVersion = asmName;
if (namespaceStr.Contains(", Version=")) //the assembly was specified by its full name
break; //stop searching for the higher version
}
while (asmName != null);
if (highestVersion != "")
{
string asmLocation = AssemblyCache.QueryAssemblyInfo(highestVersion);
retval.Add(asmLocation);
}
}
catch
{
//If exception is thrown it is very likely it is because where fusion.dll does not exist/unavailable/broken.
//We might be running under the MONO run-time.
}
#if net1
if (retval.Count == 0 && namespaceStr.ToLower().EndsWith(".dll"))
retval.Add(namespaceStr); //in case of if the namespaceStr is a dll name
return (string[])retval.ToArray(typeof(string));
#else
if (retval.Count == 0 && namespaceStr.EndsWith(".dll", StringComparison.CurrentCultureIgnoreCase))
retval.Add(namespaceStr); //in case of if the namespaceStr is a dll name
return retval.ToArray();
#endif
}
#endregion Class public methods...
/// <summary>
/// Search for namespace into local assembly file.
/// </summary>
static public bool IsNamespaceDefinedInAssembly(string asmFileName, string namespaceStr)
{
if (File.Exists(asmFileName))
{
try
{
//non reflection base assembly inspection can be found here: http://ccimetadata.codeplex.com/
//also there are some indications that Reflector uses ILReader without reflection: http://blogs.msdn.com/haibo_luo/default.aspx?p=3
//Potential solutions: AsmReader in this file or Assembly.Load(byte[]);
#if net1
Assembly assembly = Assembly.LoadFrom(asmFileName);
#else
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(asmFileName);
#endif
if (assembly != null)
{
foreach (Module m in assembly.GetModules())
{
foreach (Type t in m.GetTypes())
{
if (namespaceStr == t.Namespace)
{
return true;
}
}
}
}
}
catch { }
}
return false;
}
private bool ProbeAssembly(string file)
{
try
{
#if net1
Assembly.LoadFrom(Path.GetFullPath(file));
#else
Assembly.ReflectionOnlyLoadFrom(Path.GetFullPath(file));
#endif
return true;
}
catch (Exception)
{
return false;
}
}
//public class AsmReader : MarshalByRefObject
//{
// //Allows working with assembly loaded in a separate AppDomain
// //After processing the assembly and its domain automatically unloaded
// static public object Read(string file, Func<Assembly, object> routine)
// {
// var appDomain = AppDomain.CreateDomain("", null, new AppDomainSetup());
// object obj = appDomain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(AsmReader).ToString());
// var retval = (obj as AsmReader).ReadInternal(file, routine);
// AppDomain.Unload(appDomain);
// return retval;
// }
// object ReadInternal(string file, Func<Assembly, object> routine)
// {
// return routine(Assembly.LoadFrom(file));
// }
//}
}
}