/// <summary>
        ///     Discover instances of Visual Studio and register the first one. See <see cref="RegisterInstance" />.
        /// </summary>
        /// <returns>Instance of Visual Studio found and registered.</returns>
        public static MSBuildInstance RegisterDefaults()
        {
            MSBuildInstance instance = GetVSInstances().FirstOrDefault() as MSBuildInstance;

            if (instance == null)
            {
                instance = GetSdkInstances().FirstOrDefault();
            }
            RegisterInstance(instance);

            return(instance);
        }
        /// <summary>
        ///     Add assembly resolution for Microsoft.Build core dlls in the current AppDomain from the specified
        ///     instance of a .NET SDK version.
        /// </summary>
        /// <param name="instance"></param>
        public static void RegisterInstance(MSBuildInstance instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            AppDomain.CurrentDomain.AssemblyResolve += (_, eventArgs) =>
            {
                var assemblyName = new AssemblyName(eventArgs.Name);
                if (s_msBuildAssemblies.Contains(assemblyName.Name, StringComparer.OrdinalIgnoreCase))
                {
                    var targetAssembly = Path.Combine(instance.MSBuildPath, assemblyName.Name + ".dll");
                    return(File.Exists(targetAssembly) ? Assembly.LoadFrom(targetAssembly) : null);
                }

                return(null);
            };
        }