diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.cs index 73bd82e0fe9c..9bd4d8d2cb53 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Reflection.Metadata; +using System.Reflection.PortableExecutable; namespace Microsoft.NET.Build.Tasks.ConflictResolution { @@ -30,5 +32,32 @@ public static Version TryGetAssemblyVersion(string sourcePath) return s_assemblyExtensions.Contains(extension) ? GetAssemblyVersion(sourcePath) : null; } + private static Version GetAssemblyVersion(string sourcePath) + { + using (var assemblyStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)) + { + Version result = null; + try + { + using (PEReader peReader = new PEReader(assemblyStream, PEStreamOptions.LeaveOpen)) + { + if (peReader.HasMetadata) + { + MetadataReader reader = peReader.GetMetadataReader(); + if (reader.IsAssembly) + { + result = reader.GetAssemblyDefinition().Version; + } + } + } + } + catch (BadImageFormatException) + { + // not a PE + } + + return result; + } + } } } diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.net45.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.net45.cs deleted file mode 100644 index 8419c4c36d83..000000000000 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.net45.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#if NET46 - -using System; -using System.Reflection; - -namespace Microsoft.NET.Build.Tasks.ConflictResolution -{ - static partial class FileUtilities - { - private static Version GetAssemblyVersion(string sourcePath) - { - try - { - return AssemblyName.GetAssemblyName(sourcePath)?.Version; - } - catch(BadImageFormatException) - { - return null; - } - } - } -} - -#endif \ No newline at end of file diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.netstandard.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.netstandard.cs deleted file mode 100644 index 22c16a588ba6..000000000000 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ConflictResolution/FileUtilities.netstandard.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#if NETCOREAPP1_0 - -using System; -using System.IO; -using System.Reflection.Metadata; -using System.Reflection.PortableExecutable; - -namespace Microsoft.NET.Build.Tasks.ConflictResolution -{ - static partial class FileUtilities - { - private static Version GetAssemblyVersion(string sourcePath) - { - using (var assemblyStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)) - { - Version result = null; - try - { - using (PEReader peReader = new PEReader(assemblyStream, PEStreamOptions.LeaveOpen)) - { - if (peReader.HasMetadata) - { - MetadataReader reader = peReader.GetMetadataReader(); - if (reader.IsAssembly) - { - result = reader.GetAssemblyDefinition().Version; - } - } - } - } - catch (BadImageFormatException) - { - // not a PE - } - - return result; - } - } - } -} - -#endif \ No newline at end of file diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/Microsoft.NET.Build.Tasks.csproj b/src/Tasks/Microsoft.NET.Build.Tasks/Microsoft.NET.Build.Tasks.csproj index 0919caa27a48..10c34be98ed3 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/Microsoft.NET.Build.Tasks.csproj +++ b/src/Tasks/Microsoft.NET.Build.Tasks/Microsoft.NET.Build.Tasks.csproj @@ -35,7 +35,7 @@ $(MsBuildPackagesVersion) Runtime - +