forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDI.cs
More file actions
99 lines (85 loc) · 3.53 KB
/
DI.cs
File metadata and controls
99 lines (85 loc) · 3.53 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Runtime.InteropServices;
using Xamarin.Essentials;
namespace System
{
/// <summary>
/// 依赖注入服务组(DependencyInjection)
/// </summary>
public static class DI
{
static IServiceProvider? value;
internal static bool IsInit => value != null;
internal static IServiceProvider Value => value ?? throw new NullReferenceException("init must be called.");
/// <summary>
/// 初始化依赖注入服务组(通过配置服务项的方式)
/// </summary>
/// <param name="configureServices"></param>
public static void Init(Action<IServiceCollection> configureServices)
{
var services = new ServiceCollection();
configureServices(services);
Init(services.BuildServiceProvider());
}
/// <summary>
/// 初始化依赖注入服务组(直接赋值)
/// </summary>
/// <param name="serviceProvider"></param>
public static void Init(IServiceProvider serviceProvider)
{
value = serviceProvider;
}
/// <inheritdoc cref="System.Platform"/>
public static Platform Platform { get; }
/// <inheritdoc cref="System.DeviceIdiom"/>
public static DeviceIdiom DeviceIdiom { get; private set; }
/// <summary>
/// 当前是否使用Mono运行时
/// </summary>
public static bool IsRunningOnMono { get; }
const string DesktopWindowTypeNames =
"Avalonia.Controls.Window, Avalonia.Controls" +
"\n" +
"System.Windows.Forms.Form, System.Windows.Forms" +
"\n" +
"System.Windows.Window, PresentationFramework";
static DI()
{
IsRunningOnMono = Type.GetType("Mono.Runtime") != null;
static Platform RuntimeInformationOSPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Platform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return Platform.Apple;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return Platform.Linux;
return Platform.Unknown;
}
static DeviceIdiom GetDeviceIdiom()
{
if (DesktopWindowTypeNames.Split('\n')
.Any(x => Type.GetType(x) != null))
return DeviceIdiom.Desktop;
return DeviceIdiom.Unknown;
}
Platform = DeviceInfo.Platform.Convert();
if (Platform == Platform.Unknown) Platform = RuntimeInformationOSPlatform();
DeviceIdiom = DeviceInfo.Idiom.Convert();
if (DeviceIdiom == DeviceIdiom.Unknown) DeviceIdiom = GetDeviceIdiom();
}
/// <summary>
/// 获取依赖注入服务
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Get<T>() where T : notnull => Value.GetRequiredService<T>();
/// <inheritdoc cref="Get{T}"/>
public static T? Get_Nullable<T>() where T : notnull => Value.GetService<T>();
/// <inheritdoc cref="Get{T}"/>
public static object Get(Type serviceType) => Value.GetRequiredService(serviceType);
/// <inheritdoc cref="Get_Nullable{T}"/>
public static object? Get_Nullable(Type serviceType) => Value.GetService(serviceType);
}
}