Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit ee66a51

Browse files
committed
Change Environment.Version to return product version (dotnet/coreclr#22664)
* Change Environment.Version to return product version - Contributes to https://github.com/dotnet/corefx/issues/31099 - Use AssemblyInformationalVersion attribute as fallback * Add sanity test for Environment.Version * Disable CodeDom tests * Fix test assembly name Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
1 parent bd52650 commit ee66a51

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

Diff for: src/Common/src/CoreLib/System/Environment.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,30 @@ public static OperatingSystem OSVersion
129129

130130
public static bool UserInteractive => true;
131131

132-
// Previously this represented the File version of mscorlib.dll. Many other libraries in the framework and outside took dependencies on the first three parts of this version
133-
// remaining constant throughout 4.x. From 4.0 to 4.5.2 this was fine since the file version only incremented the last part. Starting with 4.6 we switched to a file versioning
134-
// scheme that matched the product version. In order to preserve compatibility with existing libraries, this needs to be hard-coded.
135-
public static Version Version => new Version(4, 0, 30319, 42000);
132+
public static Version Version
133+
{
134+
get
135+
{
136+
// FX_PRODUCT_VERSION is expected to be set by the host
137+
string versionString = (string)AppContext.GetData("FX_PRODUCT_VERSION");
138+
139+
if (versionString == null)
140+
{
141+
// Use AssemblyInformationalVersionAttribute as fallback if the exact product version is not specified by the host
142+
versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
143+
}
144+
145+
ReadOnlySpan<char> versionSpan = versionString.AsSpan();
146+
147+
// Strip optional suffixes
148+
int separatorIndex = versionSpan.IndexOfAny("-+ ");
149+
if (separatorIndex != -1)
150+
versionSpan = versionSpan.Slice(0, separatorIndex);
151+
152+
// Return zeros rather then failing if the version string fails to parse
153+
return Version.TryParse(versionSpan, out Version version) ? version : new Version();
154+
}
155+
}
136156

137157
public static long WorkingSet
138158
{

0 commit comments

Comments
 (0)