RuntimeInfo.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Avalonia.Platform;
  4. namespace Avalonia.Shared.PlatformSupport
  5. {
  6. internal partial class StandardRuntimePlatform
  7. {
  8. private static readonly Lazy<RuntimePlatformInfo> Info = new Lazy<RuntimePlatformInfo>(() =>
  9. {
  10. var isMono = Type.GetType("Mono.Runtime") != null;
  11. var isUnix = Environment.OSVersion.Platform == PlatformID.Unix ||
  12. Environment.OSVersion.Platform == PlatformID.MacOSX;
  13. return new RuntimePlatformInfo
  14. {
  15. IsCoreClr = false,
  16. IsDesktop = true,
  17. IsDotNetFramework = !isMono,
  18. IsMono = isMono,
  19. IsMobile = false,
  20. IsUnix = isUnix,
  21. OperatingSystem = isUnix ? DetectUnix() : OperatingSystemType.WinNT,
  22. };
  23. });
  24. [DllImport("libc")]
  25. static extern int uname(IntPtr buf);
  26. static OperatingSystemType DetectUnix()
  27. {
  28. var buffer = Marshal.AllocHGlobal(0x1000);
  29. uname(buffer);
  30. var unixName = Marshal.PtrToStringAnsi(buffer);
  31. Marshal.FreeHGlobal(buffer);
  32. if(unixName=="Darwin")
  33. return OperatingSystemType.OSX;
  34. if (unixName == "Linux")
  35. return OperatingSystemType.Linux;
  36. return OperatingSystemType.Unknown;
  37. }
  38. public RuntimePlatformInfo GetRuntimeInfo() => Info.Value;
  39. }
  40. }