Skip to content

Commit

Permalink
Use AssemblyName.Clone when possible.
Browse files Browse the repository at this point in the history
AssemblyName.Clone is not available in early versions of .NET Core.
Until we can upgrade MSBuild to use .NET Core 2.0, we can't call the
Clone method directly. This change will attempt to call .Clone using
reflection and if not available fallback to the previous .NET Core
behavior.

Potential fix for dotnet#2015 (when running on .NET Core 2.0).
  • Loading branch information
AndyGerlicher committed Apr 26, 2017
1 parent 38fe952 commit 2a240a7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/Shared/AssemblyNameExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal enum PartialComparisonFlags : int
/// between the two is done lazily on demand.
/// </summary>
[Serializable]
sealed internal class AssemblyNameExtension
internal sealed class AssemblyNameExtension
{
private AssemblyName asAssemblyName = null;
private string asString = null;
Expand Down Expand Up @@ -592,11 +592,7 @@ internal AssemblyNameExtension Clone()

if (asAssemblyName != null)
{
#if FEATURE_ASSEMBLYNAME_CLONE
newExtension.asAssemblyName = (AssemblyName)asAssemblyName.Clone();
#else
newExtension.asAssemblyName = new AssemblyName(asAssemblyName.FullName);
#endif
newExtension.asAssemblyName = asAssemblyName.CloneIfPossible();
}

newExtension.asString = asString;
Expand Down
29 changes: 25 additions & 4 deletions src/Shared/AssemblyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace Microsoft.Build.Shared
/// </summary>
internal static class AssemblyUtilities
{
private static readonly Lazy<MethodInfo> _assemblyNameCloneMethod =
new Lazy<MethodInfo>(() => typeof(AssemblyName).GetMethod("Clone"));

private static readonly Lazy<PropertyInfo> _assemblylocationProperty =
new Lazy<PropertyInfo>(() => typeof(Assembly).GetProperty("Location", typeof(string)));

public static string GetAssemblyLocation(Assembly assembly)
{
#if FEATURE_ASSEMBLY_LOCATION
Expand All @@ -19,14 +25,12 @@ public static string GetAssemblyLocation(Assembly assembly)
// Assembly.Location is only available in .netstandard1.5, but MSBuild needs to target 1.3.
// use reflection to access the property

var locationProperty = assembly.GetType().GetProperty("Location", typeof(string));

if (locationProperty == null)
if (_assemblylocationProperty.Value == null)
{
throw new NotSupportedException("Type Assembly does not have the Location property");
}

return (string)locationProperty.GetValue(assembly);
return (string)_assemblylocationProperty.Value.GetValue(assembly);
#endif
}

Expand All @@ -39,5 +43,22 @@ public static Type GetTypeInfo(this Type t)
return t;
}
#endif

public static AssemblyName CloneIfPossible(this AssemblyName assemblyNameToClone)
{
#if FEATURE_ASSEMBLYNAME_CLONE
return (AssemblyName) assemblyNameToClone.Clone();
#else
AssemblyName result = null;
try
{
result = (AssemblyName)_assemblyNameCloneMethod.Value?.Invoke(assemblyNameToClone, null);
}
catch (MemberAccessException)
{ }

return result ?? new AssemblyName(assemblyNameToClone.FullName);
#endif
}
}
}
22 changes: 5 additions & 17 deletions src/Tasks/AppConfig/DependentAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,23 @@ internal sealed class DependentAssembly
private BindingRedirect[] _bindingRedirects = null;

/// <summary>
/// The partial assemblyname, there should be no version.
/// The partial <see cref="AssemblyName"/>, there should be no version.
/// </summary>
private AssemblyName _partialAssemblyName = null;
private AssemblyName _partialAssemblyName;

/// <summary>
/// The partial assemblyname, there should be no version.
/// The partial <see cref="AssemblyName"/>, there should be no version.
/// </summary>
internal AssemblyName PartialAssemblyName
{
set
{
#if FEATURE_ASSEMBLYNAME_CLONE
_partialAssemblyName = (AssemblyName)value.Clone();
#else
_partialAssemblyName = new AssemblyName(value.FullName);
#endif
_partialAssemblyName = value.CloneIfPossible();
_partialAssemblyName.Version = null;
}
get
{
if (_partialAssemblyName == null)
{
return null;
}
#if FEATURE_ASSEMBLYNAME_CLONE
return (AssemblyName)_partialAssemblyName.Clone();
#else
return new AssemblyName(_partialAssemblyName.FullName);
#endif
return _partialAssemblyName?.CloneIfPossible();
}
}

Expand Down
19 changes: 3 additions & 16 deletions src/Tasks/AssemblyDependency/ReferenceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,14 +1070,9 @@ IEnumerable<AssemblyNameExtension> preUnificationAssemblyNames
{
foreach (AssemblyNameExtension preUnificationAssemblyName in preUnificationAssemblyNames)
{
string name = preUnificationAssemblyName.Name;
// First, unify the assembly name so that we're dealing with the right version.
// Not AssemblyNameExtension because we're going to write to it.
#if FEATURE_ASSEMBLYNAME_CLONE
AssemblyNameExtension dependentAssembly = new AssemblyNameExtension((AssemblyName)preUnificationAssemblyName.AssemblyName.Clone());
#else
AssemblyNameExtension dependentAssembly = new AssemblyNameExtension(new AssemblyName(preUnificationAssemblyName.AssemblyName.FullName));
#endif
AssemblyNameExtension dependentAssembly = new AssemblyNameExtension(preUnificationAssemblyName.AssemblyName.CloneIfPossible());

Version unifiedVersion;
bool isPrerequisite;
Expand Down Expand Up @@ -1883,11 +1878,7 @@ out AssemblyNameReference[] conflictingReferences
byte[] pkt = assemblyName.GetPublicKeyToken();
if (pkt != null && pkt.Length > 0)
{
#if FEATURE_ASSEMBLYNAME_CLONE
AssemblyName baseKey = (AssemblyName)assemblyName.AssemblyName.Clone();
#else
AssemblyName baseKey = new AssemblyName(assemblyName.AssemblyName.FullName);
#endif
AssemblyName baseKey = assemblyName.AssemblyName.CloneIfPossible();
Version version = baseKey.Version;
baseKey.Version = null;
string key = baseKey.ToString();
Expand Down Expand Up @@ -2377,11 +2368,7 @@ out string redistName
foreach (DependentAssembly remappedAssembly in _remappedAssemblies)
{
// First, exclude anything without the simple name match
#if FEATURE_ASSEMBLYNAME_CLONE
AssemblyNameExtension comparisonAssembly = new AssemblyNameExtension((AssemblyName)remappedAssembly.PartialAssemblyName.Clone());
#else
AssemblyNameExtension comparisonAssembly = new AssemblyNameExtension(new AssemblyName(remappedAssembly.PartialAssemblyName.FullName));
#endif
AssemblyNameExtension comparisonAssembly = new AssemblyNameExtension(remappedAssembly.PartialAssemblyName.CloneIfPossible());
if (assemblyName.CompareBaseNameTo(comparisonAssembly) == 0)
{
// Comparison assembly is a partial name. Give it our version.
Expand Down

0 comments on commit 2a240a7

Please sign in to comment.