-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentryRuntime.cs
182 lines (163 loc) · 4.7 KB
/
SentryRuntime.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace Sentry.PlatformAbstractions;
/// <summary>
/// Details of the runtime
/// </summary>
public class Runtime : IEquatable<Runtime>
{
private static Lazy<Runtime> _currentRuntime = new(RuntimeInfo.GetRuntime);
/// <summary>
/// Gets the current runtime
/// </summary>
/// <value>
/// The current runtime.
/// </value>
public static Runtime Current => _currentRuntime.Value;
/// <summary>
/// The name of the runtime
/// </summary>
/// <example>
/// .NET Framework, .NET Native, Mono
/// </example>
public string? Name { get; }
/// <summary>
/// The version of the runtime
/// </summary>
/// <example>
/// 4.7.2633.0
/// </example>
public string? Version { get; }
#if NETFRAMEWORK
/// <summary>
/// The .NET Framework installation which is running the process
/// </summary>
/// <value>
/// The framework installation or null if not running .NET Framework
/// </value>
public FrameworkInstallation? FrameworkInstallation { get; }
#endif
/// <summary>
/// The raw value parsed to extract Name and Version
/// </summary>
/// <remarks>
/// This property will contain a value when the underlying API
/// returned Name and Version as a single string which required parsing.
/// </remarks>
public string? Raw { get; }
/// <summary>
/// The .NET Runtime Identifier of the runtime
/// </summary>
/// <remarks>
/// This property will be populated for .NET 5 and newer, or <c>null</c> otherwise.
/// </remarks>
public string? Identifier { get; }
/// <summary>
/// Creates a new Runtime instance
/// </summary>
#if NETFRAMEWORK
public Runtime(
string? name = null,
string? version = null,
FrameworkInstallation? frameworkInstallation = null,
string? raw = null)
{
Name = name;
Version = version;
FrameworkInstallation = frameworkInstallation;
Raw = raw;
Identifier = null;
}
#else
public Runtime(
string? name = null,
string? version = null,
string? raw = null,
string? identifier = null)
{
Name = name;
Version = version;
Raw = raw;
Identifier = identifier;
}
#endif
/// <summary>
/// The string representation of the Runtime
/// </summary>
public override string? ToString()
{
if (Name == null && Version == null)
{
return Raw;
}
if (Name != null && Version == null)
{
return Raw?.Contains(Name) == true
? Raw
: $"{Name} {Raw}";
}
return $"{Name} {Version}";
}
/// <summary>
/// Compare instances for equality.
/// </summary>
/// <param name="other">The instance to compare against.</param>
/// <returns>True if the instances are equal by reference or its state.</returns>
public bool Equals(Runtime? other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return string.Equals(Name, other.Name)
&& string.Equals(Version, other.Version)
&& string.Equals(Raw, other.Raw)
#if NETFRAMEWORK
&& Equals(FrameworkInstallation, other.FrameworkInstallation);
#else
&& Equals(Identifier, other.Identifier);
#endif
}
/// <summary>
/// Compare instances for equality.
/// </summary>
/// <param name="obj">The instance to compare against.</param>
/// <returns>True if the instances are equal by reference or its state.</returns>
public override bool Equals(object? obj)
{
if (obj is null)
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((Runtime)obj);
}
/// <summary>
/// Get the hashcode of this instance.
/// </summary>
/// <returns>The hashcode of the instance.</returns>
public override int GetHashCode()
{
unchecked
{
var hashCode = Name?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (Version?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Raw?.GetHashCode() ?? 0);
#if NETFRAMEWORK
hashCode = (hashCode * 397) ^ (FrameworkInstallation?.GetHashCode() ?? 0);
#else
hashCode = (hashCode * 397) ^ (Identifier?.GetHashCode() ?? 0);
#endif
return hashCode;
}
}
}