-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGacLoader.cs
66 lines (63 loc) · 2.67 KB
/
GacLoader.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
using Ivi.Visa.ConflictManager;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace IviVisaNetSample
{
#if NET5_0_OR_GREATER
/// <summary>
/// Class used to load .NET Framework assemblies located in GAC from .NET 5+
/// Requred only for expiremental using VISA.NET library in .NET 5+
/// </summary>
internal static class GacLoader
{
/// <summary>
/// Load an assembly from the GAC.
/// </summary>
/// <param name="assemblyName"></param>
/// <returns>Loaded assembly</returns>
/// <exception cref="FileNotFoundException"></exception>
public static Assembly Load(AssemblyName assemblyName)
{
var gacPaths = new[]
{
$"{Environment.GetFolderPath(Environment.SpecialFolder.Windows)}\\Microsoft.NET\\assembly\\GAC_MSIL\\{assemblyName.Name}",
$"{Environment.GetFolderPath(Environment.SpecialFolder.Windows)}\\assembly\\GAC_MSIL\\{assemblyName.Name}",
};
foreach (var folder in gacPaths.Where(f => Directory.Exists(f)))
{
foreach (var subfolder in Directory.EnumerateDirectories(folder))
{
if (subfolder.Contains(Convert.ToHexString(assemblyName.GetPublicKeyToken()), StringComparison.OrdinalIgnoreCase)
&& subfolder.Contains(assemblyName.Version.ToString(), StringComparison.OrdinalIgnoreCase))
{
var assemblyPath = Path.Combine(subfolder, assemblyName.Name + ".dll");
if (File.Exists(assemblyPath))
return Assembly.LoadFrom(assemblyPath);
}
}
}
throw new FileNotFoundException($"Assembly {assemblyName} not found.");
}
/// <summary>
/// Preloading installed VISA implementation assemblies for NET 5+
/// </summary>
public static void LoadInstalledVisaAssemblies() {
var installedVisas = new ConflictManager().GetInstalledVisas(ApiType.DotNet);
foreach (var visaLibrary in installedVisas)
{
try
{
var inst = GacLoader.Load(new AssemblyName(visaLibrary.Location.Substring(visaLibrary.Location.IndexOf(",") + 1)));
Console.WriteLine($"Loaded assembly \"{visaLibrary.FriendlyName}\".");
}
catch (Exception exception)
{
Console.WriteLine($"Failed to load assembly \"{visaLibrary.FriendlyName}\": {exception.Message}");
}
}
}
}
#endif
}