コード例 #1
0
 private RuntimeEnvironmentInfo(string runtimeName,
                                string runtimeVersion,
                                string processArchitecture,
                                string osPlatform,
                                string osArchitecture,
                                string osDescription,
                                CoreAssembyInformation coreAssembyInfo)
 {
     this.RuntimeName         = runtimeName;
     this.RuntimeVersion      = runtimeVersion;
     this.ProcessArchitecture = processArchitecture;
     this.OsPlatform          = osPlatform;
     this.OsArchitecture      = osArchitecture;
     this.OsDescription       = osDescription;
     this.CoreAssembyInfo     = coreAssembyInfo;
 }
コード例 #2
0
        private static string GetRuntimeName(CoreAssembyInformation coreAssembyInfo)
        {
            string runtimeName = null;

#if RUNTIMEINFORMATION_TYPE_AVAILABLE
            // See https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation.frameworkdescription?view=net-5.0#remarks

            string frameworkDescription = RuntimeInformation.FrameworkDescription;
            if (frameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase))
            {
                runtimeName = ".NET Native";
            }
            else if (frameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase))
            {
                runtimeName = ".NET Framework";
            }
            else if (frameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
            {
                runtimeName = ".NET Core";
            }
            else if (frameworkDescription.StartsWith(".NET 5", StringComparison.OrdinalIgnoreCase))
            {
                runtimeName = ".NET 5";
            }
#endif
            if (runtimeName == null)
            {
                if (coreAssembyInfo.IsMscorlib)
                {
                    runtimeName = ".NET Framework";
                }
                else if (coreAssembyInfo.IsSysPrivCoreLib)
                {
                    runtimeName = $"Unknown {coreAssembyInfo.Name}-based .NET-compatible runtime";
                }
            }

            if (runtimeName == null)
            {
                runtimeName = $"Unknown .NET-compatible runtime (BCL: {coreAssembyInfo.Name})";
            }

            return(runtimeName);
        }
コード例 #3
0
        private static RuntimeEnvironmentInfo CreateNew()
        {
            try
            {
                Assembly objectTypeAssembly     = (new object()).GetType().Assembly;
                string   objectTypeAssemblyName = objectTypeAssembly.GetName()?.Name ?? UnknownMoniker;
                var      coreAssembyInfo        = new CoreAssembyInformation(
                    isMscorlib: Mscorlib.Equals(objectTypeAssemblyName, StringComparison.OrdinalIgnoreCase),
                    isSysPrivCoreLib: CoreLib.Equals(objectTypeAssemblyName, StringComparison.OrdinalIgnoreCase),
                    name: objectTypeAssemblyName);

                string runtimeName         = GetRuntimeName(coreAssembyInfo);
                string runtimeVersion      = GetRuntimeVersion(coreAssembyInfo, objectTypeAssembly);
                string processArchitecture = GetProcessArchitecture();
                string osPlatform          = GetOsPlatform();
                string osArchitecture      = GetOsArchitecture();
                string osDescription       = GetOsDescription();

                return(new RuntimeEnvironmentInfo(
                           runtimeName,
                           runtimeVersion,
                           processArchitecture,
                           osPlatform,
                           osArchitecture,
                           osDescription,
                           coreAssembyInfo));
            }
            catch
            {
                return(new RuntimeEnvironmentInfo(
                           runtimeName: UnknownMoniker,
                           runtimeVersion: UnknownMoniker,
                           processArchitecture: UnknownMoniker,
                           osPlatform: UnknownMoniker,
                           osArchitecture: UnknownMoniker,
                           osDescription: UnknownMoniker,
                           coreAssembyInfo: new CoreAssembyInformation(isMscorlib: false, isSysPrivCoreLib: false, name: UnknownMoniker)));
            }
        }
コード例 #4
0
        private static string GetRuntimeVersion(CoreAssembyInformation coreAssembyInfo, Assembly objectTypeAssembly)
        {
            Version environmentVersion = Environment.Version;

            if (coreAssembyInfo.IsSysPrivCoreLib && (environmentVersion.Major == 3 || environmentVersion.Major >= 5))
            {
                // On Net Core, Environment.Version returns "4.x" in .NET Core 2.x, but it is correct since .NET Core 3.0.0.
                string runtimeVersion = environmentVersion.ToString();
                return(runtimeVersion);
            }

            string assemblyInfoVer = objectTypeAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion;

            if (assemblyInfoVer != null)
            {
                // Strip the git hash if there is one:
                int plusIndex = assemblyInfoVer.IndexOf('+');
                if (plusIndex >= 0)
                {
                    assemblyInfoVer = assemblyInfoVer.Substring(0, plusIndex);
                }

                // Servicing version if there is one:
                int minusIndex = assemblyInfoVer.IndexOf('-');
                if (minusIndex >= 0)
                {
                    assemblyInfoVer = assemblyInfoVer.Substring(0, minusIndex);
                }

                string runtimeVersion = assemblyInfoVer.Trim();
                if (runtimeVersion.Length > 0)
                {
                    return(runtimeVersion);
                }
            }

            return(UnknownMoniker);
        }