-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleInitializer.cs
35 lines (29 loc) · 1.13 KB
/
ModuleInitializer.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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ModuleInitializerAttribute : Attribute { }
}
static class ModuleInitializer
{
[ModuleInitializer]
internal static void Run()
{
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
var binPath = ThisAssembly.Project.MSBuildBinPath;
Microsoft.Build.Locator.MSBuildLocator.RegisterMSBuildPath(binPath);
// Set environment variables so SDKs can be resolved.
Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", Path.Combine(binPath, "MSBuild.exe"), EnvironmentVariableTarget.Process);
}
static Assembly? OnAssemblyResolve(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name).Name;
var file = Path.Combine(ThisAssembly.Project.MSBuildBinPath, name + ".dll");
if (name.StartsWith("Microsoft.Build") && File.Exists(file))
return Assembly.LoadFrom(file);
return null;
}
}