diff --git a/src/System.Management.Automation/engine/PSVersionInfo.cs b/src/System.Management.Automation/engine/PSVersionInfo.cs index 7140c3e243c..4f49ac8646c 100644 --- a/src/System.Management.Automation/engine/PSVersionInfo.cs +++ b/src/System.Management.Automation/engine/PSVersionInfo.cs @@ -4,8 +4,6 @@ using System.Collections; using System.Diagnostics; using System.Globalization; -using System.Management.Automation.Internal; -using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -41,7 +39,7 @@ public class PSVersionInfo internal const string PSOSName = "OS"; internal const string SerializationVersionName = "SerializationVersion"; internal const string WSManStackVersionName = "WSManStackVersion"; - private static PSVersionHashTable s_psVersionTable = null; + private static readonly PSVersionHashTable s_psVersionTable; /// /// A constant to track current PowerShell Version. @@ -54,14 +52,15 @@ public class PSVersionInfo /// For each later release of PowerShell, this constant needs to /// be updated to reflect the right version. /// - private static Version s_psV1Version = new Version(1, 0); - private static Version s_psV2Version = new Version(2, 0); - private static Version s_psV3Version = new Version(3, 0); - private static Version s_psV4Version = new Version(4, 0); - private static Version s_psV5Version = new Version(5, 0); - private static Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); - private static SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, null, null); - private static SemanticVersion s_psV7Version; + private static readonly Version s_psV1Version = new Version(1, 0); + private static readonly Version s_psV2Version = new Version(2, 0); + private static readonly Version s_psV3Version = new Version(3, 0); + private static readonly Version s_psV4Version = new Version(4, 0); + private static readonly Version s_psV5Version = new Version(5, 0); + private static readonly Version s_psV51Version = new Version(5, 1, NTVerpVars.PRODUCTBUILD, NTVerpVars.PRODUCTBUILD_QFE); + private static readonly SemanticVersion s_psV6Version = new SemanticVersion(6, 0, 0, preReleaseLabel: null, buildLabel: null); + private static readonly SemanticVersion s_psSemVersion; + private static readonly Version s_psVersion; /// /// A constant to track current PowerShell Edition. @@ -100,12 +99,13 @@ static PSVersionInfo() rawGitCommitId = mainVersion; } - s_psV7Version = new SemanticVersion(mainVersion); + s_psSemVersion = new SemanticVersion(mainVersion); + s_psVersion = (Version)s_psSemVersion; - s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV7Version; + s_psVersionTable[PSVersionInfo.PSVersionName] = s_psSemVersion; s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue; s_psVersionTable[PSGitCommitIdName] = rawGitCommitId; - s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psV7Version }; + s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version, s_psVersion }; s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion); s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion; s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion(); @@ -122,7 +122,7 @@ internal static Hashtable GetPSVersionTableForDownLevel() { var result = (Hashtable)s_psVersionTable.Clone(); // Downlevel systems don't support SemanticVersion, but Version is most likely good enough anyway. - result[PSVersionInfo.PSVersionName] = (Version)(SemanticVersion)s_psVersionTable[PSVersionInfo.PSVersionName]; + result[PSVersionInfo.PSVersionName] = s_psVersion; return result; } @@ -173,7 +173,7 @@ public static Version PSVersion { get { - return (SemanticVersion)GetPSVersionTable()[PSVersionInfo.PSVersionName]; + return s_psVersion; } } @@ -181,7 +181,7 @@ internal static string GitCommitId { get { - return (string)GetPSVersionTable()[PSGitCommitIdName]; + return (string)s_psVersionTable[PSGitCommitIdName]; } } @@ -189,7 +189,7 @@ internal static Version[] PSCompatibleVersions { get { - return (Version[])GetPSVersionTable()[PSCompatibleVersionsName]; + return (Version[])s_psVersionTable[PSCompatibleVersionsName]; } } @@ -200,7 +200,7 @@ public static string PSEdition { get { - return (string)GetPSVersionTable()[PSVersionInfo.PSEditionName]; + return (string)s_psVersionTable[PSVersionInfo.PSEditionName]; } } @@ -208,7 +208,7 @@ internal static Version SerializationVersion { get { - return (Version)GetPSVersionTable()[SerializationVersionName]; + return (Version)s_psVersionTable[SerializationVersionName]; } } @@ -272,9 +272,9 @@ internal static string FeatureVersionString internal static bool IsValidPSVersion(Version version) { - if (version.Major == s_psV7Version.Major) + if (version.Major == s_psSemVersion.Major) { - return version.Minor == s_psV7Version.Minor; + return version.Minor == s_psSemVersion.Minor; } if (version.Major == s_psV6Version.Major) @@ -327,9 +327,9 @@ internal static SemanticVersion PSV6Version get { return s_psV6Version; } } - internal static SemanticVersion PSV7Version + internal static SemanticVersion PSCurrentVersion { - get { return s_psV7Version; } + get { return s_psSemVersion; } } #endregion