/
EmbeddedAssemblyResolution.cs
112 lines (99 loc) · 3.97 KB
/
EmbeddedAssemblyResolution.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
// ***********************************************************************
// Copyright (c) 2014-2021 Charlie Poole, Terje Sandstrom
//
// 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.
// ***********************************************************************
#if ENABLED
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
#if !NET35
using System.Runtime.Loader;
#endif
namespace NUnit.VisualStudio.TestAdapter
{
internal static class EmbeddedAssemblyResolution
{
[ModuleInitializer]
public static void EnsureInitialized()
{
#if NET35
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
#else
AssemblyLoadContext.Default.Resolving += Default_Resolving;
#endif
}
#if NET35
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
using (var stream = TryGetResourceAssemblyStream(new AssemblyName(args.Name)))
{
return stream == null ? null : Assembly.Load(stream.ReadToEnd());
}
}
#else
private static Assembly Default_Resolving(AssemblyLoadContext context, AssemblyName name)
{
using (var stream = TryGetResourceAssemblyStream(name))
{
if (stream == null) return null;
return context.LoadFromStream(stream);
}
}
#endif
private static readonly string[] AllowedResourceAssemblyNames =
{
"Mono.Cecil"
};
private static Stream TryGetResourceAssemblyStream(AssemblyName assemblyName)
{
var properCasing = AllowedResourceAssemblyNames.FirstOrDefault(name =>
name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase));
if (properCasing == null) return null;
return typeof(EmbeddedAssemblyResolution)
#if !NET35
.GetTypeInfo()
#endif
.Assembly.GetManifestResourceStream(@"Assemblies/" + properCasing + ".dll");
}
#if NET35
private static byte[] ReadToEnd(this Stream stream)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead) throw new ArgumentException("Stream must be readable.", nameof(stream));
if (!stream.CanSeek) throw new ArgumentException("Stream must be seekable.", nameof(stream));
var array = new byte[stream.Length - stream.Position];
var position = 0;
for (int bytesRead; position < array.Length; position += bytesRead)
{
bytesRead = stream.Read(array, position, array.Length - position);
if (bytesRead == 0) break;
}
if (position == array.Length) return array;
var resized = new byte[position];
Array.Copy(array, resized, position);
return resized;
}
#endif
}
}
#endif