Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AssemblyName.Clone when possible. #2016

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 42 additions & 4 deletions src/Shared/AssemblyUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ namespace Microsoft.Build.Shared
/// </summary>
internal static class AssemblyUtilities
{
// True when the cached method info objects have been set.
private static bool s_initialized;

// Cached method info
private static MethodInfo s_assemblyNameCloneMethod;
private static PropertyInfo s_assemblylocationProperty;

public static string GetAssemblyLocation(Assembly assembly)
{
#if FEATURE_ASSEMBLY_LOCATION
return assembly.Location;
#else
// Assembly.Location is only available in .netstandard1.5, but MSBuild needs to target 1.3.
// use reflection to access the property
Initialize();

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

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

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

Expand All @@ -39,5 +45,37 @@ 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

Initialize();

if (s_assemblyNameCloneMethod == null)
{
return new AssemblyName(assemblyNameToClone.FullName);
}

// Try to Invoke the Clone method via reflection. If the method exists (it will on .NET
// Core 2.0 or later) use that result, otherwise use new AssemblyName(FullName).
return (AssemblyName) s_assemblyNameCloneMethod.Invoke(assemblyNameToClone, null) ??
new AssemblyName(assemblyNameToClone.FullName);
#endif
}

/// <summary>
/// Initialize static fields. Doesn't need to be thread safe.
/// </summary>
private static void Initialize()
{
if (s_initialized) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to be run single-threaded?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it doesn't really matter if some things race to plunk the same value down


s_assemblyNameCloneMethod = typeof(AssemblyName).GetMethod("Clone");
s_assemblylocationProperty = typeof(Assembly).GetProperty("Location", typeof(string));
s_initialized = true;
}
}
}
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