-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathPlatform.cs
75 lines (62 loc) · 2.09 KB
/
Platform.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
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
using System;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core
{
internal enum OperatingSystemType
{
Windows,
Unix,
MacOSX
}
internal static class Platform
{
public static string ProcessorArchitecture => RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
public static OperatingSystemType OperatingSystem
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OperatingSystemType.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OperatingSystemType.Unix;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OperatingSystemType.MacOSX;
}
throw new PlatformNotSupportedException();
}
}
public static string GetNativeLibraryExtension()
{
switch (OperatingSystem)
{
case OperatingSystemType.MacOSX:
return ".dylib";
case OperatingSystemType.Unix:
return ".so";
case OperatingSystemType.Windows:
return ".dll";
}
throw new PlatformNotSupportedException();
}
/// <summary>
/// Returns true if the runtime is Mono.
/// </summary>
public static bool IsRunningOnMono()
=> Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Returns true if the runtime is .NET Framework.
/// </summary>
public static bool IsRunningOnNetFramework()
=> typeof(object).Assembly.GetName().Name == "mscorlib" && !IsRunningOnMono();
/// <summary>
/// Returns true if the runtime is .NET Core.
/// </summary>
public static bool IsRunningOnNetCore()
=> typeof(object).Assembly.GetName().Name != "mscorlib";
}
}