<PackageReference Include="NUnit" Version="4.2.2" />

RuntimeFramework

public sealed class RuntimeFramework
RuntimeFramework represents a particular version of a common language runtime implementation.
using System; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal { [Serializable] [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public sealed class RuntimeFramework { public static readonly Version DefaultVersion = new Version(0, 0); private static readonly Lazy<RuntimeFramework> LazyCurrentFramework = new Lazy<RuntimeFramework>(delegate { Type type = null; Type type2 = null; try { type = Type.GetType("Mono.Runtime", false); type2 = Type.GetType("MonoTouch.UIKit.UIApplicationDelegate,monotouch", false); } catch { } bool flag = (object)type2 != null; bool flag2 = (object)type != null; bool flag3 = !flag2 && !flag && IsNetCore(); RuntimeType runtime = flag ? RuntimeType.MonoTouch : (flag2 ? RuntimeType.Mono : ((!flag3) ? RuntimeType.NetFramework : RuntimeType.NetCore)); int num = Environment.Version.Major; int minor = Environment.Version.Minor; if (flag2) { switch (num) { case 1: minor = 0; break; case 2: num = 3; minor = 5; break; } } else if (!flag3) { switch (num) { case 2: num = 3; minor = 5; break; case 4: if ((object)Type.GetType("System.Reflection.AssemblyMetadataAttribute") != null) minor = 5; break; } } else { num = 0; minor = 0; } RuntimeFramework runtimeFramework = new RuntimeFramework(runtime, new Version(num, minor)) { ClrVersion = Environment.Version }; if (flag2) { MethodInfo method = type.GetMethod("GetDisplayName", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.ExactBinding); if ((object)method != null) runtimeFramework.DisplayName = (((string)method.Invoke(null, Array.Empty<object>())) ?? "Mono"); } return runtimeFramework; }); public static RuntimeFramework CurrentFramework => LazyCurrentFramework.Value; public RuntimeType Runtime { get; } public Version FrameworkVersion { get; set; } public Version ClrVersion { get; set; } public bool AllowAnyVersion => ClrVersion == DefaultVersion; public string DisplayName { get; set; } public RuntimeFramework(RuntimeType runtime, Version version) { Runtime = runtime; if (version.Build < 0) InitFromFrameworkVersion(version); else InitFromClrVersion(version); DisplayName = GetDefaultDisplayName(runtime, version); } [MemberNotNull("FrameworkVersion")] [MemberNotNull("ClrVersion")] private void InitFromFrameworkVersion(Version version) { Version version4 = FrameworkVersion = (ClrVersion = version); if (version.Major > 0) { switch (Runtime) { case RuntimeType.SSCLI: case RuntimeType.MonoTouch: break; case RuntimeType.NetCore: ClrVersion = new Version(4, 0, 30319); break; case RuntimeType.Any: case RuntimeType.NetFramework: case RuntimeType.Mono: switch (version.Major) { case 1: switch (version.Minor) { case 0: ClrVersion = ((Runtime == RuntimeType.Mono) ? new Version(1, 1, 4322) : new Version(1, 0, 3705)); break; case 1: if (Runtime == RuntimeType.Mono) FrameworkVersion = new Version(1, 0); ClrVersion = new Version(1, 1, 4322); break; default: ThrowInvalidFrameworkVersion(version); break; } break; case 2: case 3: ClrVersion = new Version(2, 0, 50727); break; case 4: ClrVersion = new Version(4, 0, 30319); break; default: ThrowInvalidFrameworkVersion(version); break; } break; } } } private static void ThrowInvalidFrameworkVersion(Version version) { throw new ArgumentException("Unknown framework version " + version?.ToString(), "version"); } [MemberNotNull("FrameworkVersion")] [MemberNotNull("ClrVersion")] private void InitFromClrVersion(Version version) { FrameworkVersion = new Version(version.Major, version.Minor); ClrVersion = version; if (Runtime == RuntimeType.Mono && version.Major == 1) FrameworkVersion = new Version(1, 0); if (Runtime == RuntimeType.NetFramework && version.Major == 4 && version.Minor >= 5) ClrVersion = new Version(4, 0, 30319); if (Runtime == RuntimeType.NetCore) ClrVersion = new Version(4, 0, 30319); } public static RuntimeFramework Parse(string s) { RuntimeType runtimeType = RuntimeType.Any; Version version = DefaultVersion; string[] array = s.Split('-', StringSplitOptions.None); if (array.Length == 2) { runtimeType = ParseRuntimeType(array[0]); string text = array[1]; if (text != string.Empty) { version = new Version(text); if (runtimeType == RuntimeType.NetFramework && version.Major >= 5) runtimeType = RuntimeType.NetCore; } } else if (s.StartsWith("v", StringComparison.OrdinalIgnoreCase)) { version = new Version(s.Substring(1)); } else if (IsRuntimeTypeName(s)) { runtimeType = ParseRuntimeType(s); } else { version = new Version(s); } return new RuntimeFramework(runtimeType, version); } public override string ToString() { if (AllowAnyVersion) return GetShortName(Runtime, FrameworkVersion).ToLowerInvariant(); string text = FrameworkVersion.ToString(); if (Runtime == RuntimeType.Any) return "v" + text; return GetShortName(Runtime, FrameworkVersion).ToLowerInvariant() + "-" + text; } public bool Supports(RuntimeFramework target) { if (Runtime != 0 && target.Runtime != 0 && Runtime != target.Runtime) return false; if (AllowAnyVersion || target.AllowAnyVersion) return true; if (!VersionsMatch(ClrVersion, target.ClrVersion)) return false; if (FrameworkVersion.Major > target.FrameworkVersion.Major) return true; if (FrameworkVersion.Major == target.FrameworkVersion.Major) return FrameworkVersion.Minor >= target.FrameworkVersion.Minor; return false; } private static bool IsNetCore() { if (Environment.Version.Major >= 5) return true; return false; } private static RuntimeType ParseRuntimeType(string s) { if (s.ToLowerInvariant() == "net") s = "NetFramework"; return (RuntimeType)Enum.Parse(typeof(RuntimeType), s, true); } private static bool IsRuntimeTypeName(string name) { if (!(name.ToLowerInvariant() == "net")) return Enum.GetNames(typeof(RuntimeType)).Contains(name, StringComparer.OrdinalIgnoreCase); return true; } private static string GetShortName(RuntimeType runtime, Version version) { switch (runtime) { case RuntimeType.NetCore: if (version.Major >= 5) break; goto default; default: return runtime.ToString(); case RuntimeType.NetFramework: break; } return "Net"; } private static string GetDefaultDisplayName(RuntimeType runtime, Version version) { if (version == DefaultVersion) return GetShortName(runtime, version); if (runtime == RuntimeType.Any) return "v" + version?.ToString(); return GetShortName(runtime, version) + " " + version?.ToString(); } private static bool VersionsMatch(Version v1, Version v2) { if (v1.Major == v2.Major && v1.Minor == v2.Minor && (v1.Build < 0 || v2.Build < 0 || v1.Build == v2.Build)) { if (v1.Revision >= 0 && v2.Revision >= 0) return v1.Revision == v2.Revision; return true; } return false; } } }