forked from ClassIsland/ClassIsland
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIAppHost.cs
50 lines (42 loc) · 1.18 KB
/
IAppHost.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
#if !NETFRAMEWORK
using Microsoft.Extensions.Hosting;
namespace ClassIsland.Shared;
/// <summary>
/// 应用主机接口
/// </summary>
public interface IAppHost
{
/// <summary>
/// 核心库版本
/// </summary>
public static Version CoreVersion = new Version(1, 4, 0, 0);
/// <summary>
/// 应用主机
/// </summary>
public static IHost? Host;
/// <summary>
/// 获取指定的服务
/// </summary>
/// <typeparam name="T">要获取的服务类型</typeparam>
/// <returns>获取的服务</returns>
/// <exception cref="ArgumentException"></exception>
public static T GetService<T>()
{
var s = Host?.Services.GetService(typeof(T));
if (s != null)
{
return (T)s;
}
throw new ArgumentException($"Service {typeof(T)} is null!");
}
/// <summary>
/// 尝试获取指定的服务
/// </summary>
/// <typeparam name="T">要获取的服务类型</typeparam>
/// <returns>如果获取成功,则返回获取到的服务,否则返回null</returns>
public static T? TryGetService<T>()
{
return (T?)Host?.Services.GetService(typeof(T));
}
}
#endif