-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathCoreCLRAssemblyLoader.cs
50 lines (40 loc) · 1.48 KB
/
CoreCLRAssemblyLoader.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
// 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.Diagnostics;
using System.IO;
using System.Reflection;
namespace Microsoft.Build.Shared
{
/// <summary>
/// CoreCLR-compatible wrapper for loading task assemblies.
/// </summary>
internal sealed class CoreClrAssemblyLoader
{
private readonly Dictionary<string, Assembly> _pathsToAssemblies = new Dictionary<string, Assembly>(StringComparer.OrdinalIgnoreCase);
private readonly object _guard = new object();
public Assembly LoadFromPath(string fullPath)
{
if (fullPath == null)
{
throw new ArgumentNullException(nameof(fullPath));
}
Debug.Assert(Path.IsPathRooted(fullPath));
lock (_guard)
{
Assembly assembly;
if (_pathsToAssemblies.TryGetValue(fullPath, out assembly))
{
return assembly;
}
var contextForAssemblyPath = new MSBuildLoadContext(fullPath);
assembly = contextForAssemblyPath.LoadFromAssemblyPath(fullPath);
if (assembly != null)
{
_pathsToAssemblies[fullPath] = assembly;
}
return assembly;
}
}
}
}