/
OSPlatform.cs
70 lines (55 loc) · 2.02 KB
/
OSPlatform.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
namespace System.Runtime.InteropServices
{
public readonly struct OSPlatform : IEquatable<OSPlatform>
{
public static OSPlatform FreeBSD { get; } = new OSPlatform("FREEBSD");
public static OSPlatform Linux { get; } = new OSPlatform("LINUX");
public static OSPlatform OSX { get; } = new OSPlatform("OSX");
public static OSPlatform Windows { get; } = new OSPlatform("WINDOWS");
internal string Name { get; }
private OSPlatform(string osPlatform)
{
ArgumentException.ThrowIfNullOrEmpty(osPlatform);
Name = osPlatform;
}
/// <summary>
/// Creates a new OSPlatform instance.
/// </summary>
/// <remarks>If you plan to call this method frequently, please consider caching its result.</remarks>
public static OSPlatform Create(string osPlatform)
{
return new OSPlatform(osPlatform);
}
public bool Equals(OSPlatform other)
{
return Equals(other.Name);
}
internal bool Equals(string? other)
{
return string.Equals(Name, other, StringComparison.OrdinalIgnoreCase);
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is OSPlatform osPlatform && Equals(osPlatform);
}
public override int GetHashCode()
{
return Name == null ? 0 : StringComparer.OrdinalIgnoreCase.GetHashCode(Name);
}
public override string ToString()
{
return Name ?? string.Empty;
}
public static bool operator ==(OSPlatform left, OSPlatform right)
{
return left.Equals(right);
}
public static bool operator !=(OSPlatform left, OSPlatform right)
{
return !(left == right);
}
}
}