-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathRuntimeEnvironmentUtility.cs
150 lines (134 loc) · 4.43 KB
/
RuntimeEnvironmentUtility.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace Rollbar.Common
{
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.IO;
/// <summary>
/// A utility class aiding discovery of the current runtime environment.
/// </summary>
public static class RuntimeEnvironmentUtility
{
/// <summary>
/// Gets the SDK runtime location path.
/// </summary>
/// <returns>System.String.</returns>
public static string GetSdkRuntimeLocationPath()
{
#if (!NETFX || NETFX_461nNewer)
string? path = AppContext.BaseDirectory;
if (string.IsNullOrWhiteSpace(path))
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
string? sdkAssembliesPath = Path.GetDirectoryName(thisAssembly.Location);
path = sdkAssembliesPath;
}
if (path != null)
{
return path;
}
#endif
return string.Empty;
}
/// <summary>
/// Gets the type assembly product.
/// </summary>
/// <param name="theType">The type.</param>
/// <returns>System.String.</returns>
public static string GetTypeAssemblyProduct(Type theType)
{
if (theType.Assembly.GetCustomAttribute(typeof(AssemblyProductAttribute)) is AssemblyProductAttribute product)
{
return product.Product;
}
return string.Empty;
}
/// <summary>
/// Gets the type assembly version.
/// </summary>
/// <param name="theType">The type.</param>
/// <returns></returns>
public static string GetTypeAssemblyVersion(Type theType)
{
if (theType.Assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)) is AssemblyInformationalVersionAttribute infoVersion)
{
return infoVersion.InformationalVersion;
}
return string.Empty;
}
/// <summary>
/// Gets the assembly target frameworks.
/// </summary>
/// <param name="typeFromAssembly">The type from assembly.</param>
/// <returns></returns>
public static string[] GetAssemblyTargetFrameworks(Type typeFromAssembly)
{
return RuntimeEnvironmentUtility.GetAssemblyTargetFrameworks(typeFromAssembly.Assembly);
}
/// <summary>
/// Gets the assembly target frameworks.
/// </summary>
/// <param name="theAssembly">The assembly.</param>
/// <returns></returns>
public static string[] GetAssemblyTargetFrameworks(Assembly theAssembly)
{
var attributes = theAssembly
.GetCustomAttributes(typeof(TargetFrameworkAttribute), false)
.Cast<TargetFrameworkAttribute>()
.ToArray();
var targetFrameworks = attributes
.Select(a=>a.FrameworkName)
.ToArray();
return targetFrameworks;
}
/// <summary>
/// Gets the hosting CLR version.
/// </summary>
/// <returns>Version.</returns>
public static Version GetHostingClrVersion()
{
var ver = Environment.Version;
return ver;
}
/// <summary>
/// Gets the dot net runtime description.
/// </summary>
/// <returns>System.String.</returns>
public static string GetDotNetRuntimeDescription()
{
#if NETFX_47nOlder
string dotNetRuntime = $".NET {Environment.Version}";
#else
string dotNetRuntime = RuntimeInformation.FrameworkDescription;
#endif
return dotNetRuntime;
}
/// <summary>
/// Gets the OS description.
/// </summary>
/// <returns></returns>
public static string GetOSDescription()
{
#if NETFX_47nOlder
return Environment.OSVersion.VersionString;
#else
return RuntimeInformation.OSDescription;
#endif
}
/// <summary>
/// Gets the CPU architecture.
/// </summary>
/// <returns></returns>
public static string? GetCpuArchitecture()
{
#if NETFX_47nOlder
return null;
#else
return RuntimeInformation.OSArchitecture.ToString();
#endif
}
}
}