I'm retrieving the operating system platform with .NET Core with system.runtime.interopservices
I have created the code below and was wondering if there was any other way that I could improve it since I haven't been able to get a switch statement to work in the OperatingSystem class.
/// <summary>
/// Operating System Class
/// For Retrieving The OS
/// Platform.
/// AE : 9/08/2018
/// </summary>
class OperatingSystem
{
/// <summary>
/// Operating System Platforms.
/// </summary>
public enum OSPlatform
{
Windows,
OSX,
Linux,
Not_Supported
}
/// <summary>
/// Return Operating System
/// </summary>
public static OSPlatform GetPlatform
{
// Switch Statement Not Compatitable
get
{
if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
return OSPlatform.Windows;
}
else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
return OSPlatform.OSX;
}
else if (RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
return OSPlatform.Linux;
}
else
{
return OSPlatform.Not_Supported;
}
}
}
}
The class is being called like this,
static void Main()
{
switch (OperatingSystem.GetPlatform)
{
case OperatingSystem.OSPlatform.Windows:
Console.WriteLine("Goto Windows Class");
break;
case OperatingSystem.OSPlatform.OSX:
Console.WriteLine("Goto OSX Class");
break;
case OperatingSystem.OSPlatform.Linux:
Console.WriteLine("Goto Linux Class");
break;
default:
Console.WriteLine("OS Not Supported");
break;
}
Console.ReadKey();
}
1 Answer 1
Review
- Don't use underscores in enum values
NotSupported
- The enum value with integer value 0 represents its default value; I would make this
NotSupported
in order to avoid some bias GetPlatform
is a method name; preferPlatform
as property name- Since the platform won't change while your program is running, consider using a
Lazy
to retrieve the platform once.
Don't like if-statements?
Let's first refactor the enum to match aforementioned conventions:
public enum OSPlatform
{
NotSupported,
Windows,
OSX,
Linux,
}
We could then use a Lazy
:
static readonly Lazy<OSPlatform> platform = new Lazy<OSPlatform>(GetPlatform, true);
public static OSPlatform Platform => platform.Value;
And refactor the intitial method using a helper generator using a simple ValueTuple
to avoid the boiler-plate if-statements.
private static IEnumerable
<(OSPlatform Platform, RuntimeOSPlatform RuntimePlatform)?> EnumeratePlatforms()
{
yield return (OSPlatform.Windows, RuntimeOSPlatform.Windows);
yield return (OSPlatform.OSX, RuntimeOSPlatform.OSX);
yield return (OSPlatform.Linux, RuntimeOSPlatform.Linux);
}
private static OSPlatform GetPlatform()
{
return EnumeratePlatforms().FirstOrDefault(p
=> RuntimeInformation.IsOSPlatform(p.Value.RuntimePlatform))?.Platform ?? default;
}
I've created an alias to avoid redundant namespace code:
using RuntimeOSPlatform = System.Runtime.InteropServices.OSPlatform;
If you want to have some fun, you could refactor this solution to use Expression
and some reflection to avoid creating hard-coded tuples.
-
1\$\begingroup\$ You're writing answers at a gruelling pace :-O I cannot keep up reading them so there's a small voting lag lol \$\endgroup\$t3chb0t– t3chb0t2019年08月29日 17:16:56 +00:00Commented Aug 29, 2019 at 17:16
-
\$\begingroup\$ @t3chb0t Yeah I checked you weren't only for 27 minutes o_O Please keep up :/ lol :p \$\endgroup\$dfhwze– dfhwze2019年08月29日 17:18:26 +00:00Commented Aug 29, 2019 at 17:18
-
1\$\begingroup\$ I'll try but my tokenizer is driving me crazy :P I have more ideas so I'll post a new question soon; I'm adding a context to it and... no more spoilers ;-] \$\endgroup\$t3chb0t– t3chb0t2019年08月29日 17:20:41 +00:00Commented Aug 29, 2019 at 17:20
-
\$\begingroup\$ I see you have enough green internet points for today... let's save some for tomorrow. \$\endgroup\$t3chb0t– t3chb0t2019年08月29日 18:15:04 +00:00Commented Aug 29, 2019 at 18:15
switch
es? I find it's a bad idea to duplicate those values without a very good reason. \$\endgroup\$