`OperatingSystem.IsAndroid()` Alternative for .NET Standard 2.0
I am moving a library from .NET 8 to .NET Standard 2.0 since I would like to use it in a legacy .NET Framework project as well.
The only issue that I haven't been able to figure out a good way to resolve is
OperatingSystem.IsAndroid()
. This library includes an API client in it for our backend, so it gets used in both Windows desktop and an Android/iOS MAUI app and I use that to add a header so I know which platform the request came from. Right now I've replaced that with RuntimeInformation.OSDescription.ToLower().Contains("android")
, but this feels extremely fragile to me so I'm a bit worried with keeping it that way long term. Are there any good (non 3rd party nuget package) alternatives or ways to use OperatingSystem.IsAndroid()
?
I had to add the System.Text.Json library to get those classes back and am just not sure if there is a similar one for this OperatingSystem class. The big issue I'm running into trying to find info on this is everything is recommending using RuntimeInformation.IsOSPlatform(OSPlatform.*)
, however this does not contain a platform for android.34 Replies
Could maybe use pragmas?
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Alternatively, bite the bullet, update the legacy project, and never have to worry about .NET Fx existing
Thats not possible
This legacy application was first written in 2000-somthing and started on Framework 2.0, its old and extremely fragile. Its slowly being replaced with a .NET 8 API, but it takes time
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
I think
#if NET5_0_OR_GREATER
would always be false at compile time, since its being built with standard 2.0Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
again, upgrading the legacy app from .NET framework is not possible
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
yeah I get that, not trying to do it in the framework app. Trying in .NET Standard
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Standard is the lowest common denominator
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
If something does not exist on Fx, it does not exist on Standard 2.0
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
The link you sent says to use OperatingSystem.IsAndroid(), which doesn't exist in .net standard
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Yeah I looked in that repo to see what this class did, coped it and it doesn't work
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs#L201
GitHub
runtime/src/libraries/System.Private.CoreLib/src/System/OperatingSy...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Not sure what you mean by one level deeper, theres nothing to step into here
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
if android doesn't exist for .NET Standard 2.0, then why does it run fine on android? That seems a bit odd to me, for it to run there fine but not know about its existence?
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
I have been working with .NET Framework stuff since about 2020 and started migrating parts of this app to .NET in 2022, this is my first time trying to do anything with .NET Standard since I've never had to share code between them before
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Ok so I think I understand from your explanation
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
the other alternative is to not use netstandard2.0 and to multitarget and use
#if
insteadUnknown User•6mo ago
Message Not Public
Sign In & Join Server To View
i mean, it's a netstandard2.0 libary, i think it is acceptable that the library considers multiple possible platforms that it can be used on
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Yeah, let me try to give a better explanation
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Currently we have:
- Legacy monolithic ASP.NET application (.NET Framework)
- Legacy Xamarin app
- New API (.NET 8)
- New API client library (.NET 8)
- New MAUI app < -- Uses the New API client library
- New Blazor Web (.NET 8) < -- Uses the New API client library
And would like to add the API Client library to the legacy application so we can start moving more parts of it to the new API
And in the API client library we add a header to all requests that say which platform the request is coming from, currently using the
OperatingSystem.IsAndroid()
method
No problem at all, thank you for your help!