dotnet-installer/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs

65 lines
2 KiB
C#
Raw Normal View History

2015-12-17 15:04:18 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
namespace Microsoft.Extensions.DependencyModel
{
public class DependencyContext
{
private const string DepsResourceSufix = ".deps.json";
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
public DependencyContext(string target, string runtime, CompilationOptions compilationOptions, CompilationLibrary[] compileLibraries, RuntimeLibrary[] runtimeLibraries)
2015-12-17 15:04:18 -08:00
{
Target = target;
Runtime = runtime;
CompilationOptions = compilationOptions;
2015-12-17 15:04:18 -08:00
CompileLibraries = compileLibraries;
RuntimeLibraries = runtimeLibraries;
}
public static DependencyContext Default => _defaultContext.Value;
public string Target { get; }
2015-12-17 15:04:18 -08:00
public string Runtime { get; }
public CompilationOptions CompilationOptions { get; }
2015-12-17 15:04:18 -08:00
public IReadOnlyList<CompilationLibrary> CompileLibraries { get; }
2015-12-17 15:04:18 -08:00
public IReadOnlyList<RuntimeLibrary> RuntimeLibraries { get; }
2015-12-17 15:04:18 -08:00
private static DependencyContext LoadDefault()
2015-12-17 15:04:18 -08:00
{
var entryAssembly = Assembly.GetEntryAssembly();
return Load(entryAssembly);
}
public static DependencyContext Load(Assembly assembly)
{
var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + DepsResourceSufix);
2015-12-17 15:04:18 -08:00
if (stream == null)
{
return null;
2015-12-17 15:04:18 -08:00
}
using (stream)
{
return Load(stream);
}
}
public static DependencyContext Load(Stream stream)
{
return new DependencyContextReader().Read(stream);
}
}
}