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

Conversation

AndyGerlicher
Copy link
Contributor

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 #2015 (when running on .NET Core 2.0).

#else
newExtension.asAssemblyName = new AssemblyName(asAssemblyName.FullName);
#endif
newExtension.asAssemblyName = asAssemblyName.Clone2();
Copy link
Member

Choose a reason for hiding this comment

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

CloneIfPossible?

AssemblyName result = null;
try
{
MethodInfo method = assemblyNameToClone?.GetType().GetMethod("Clone");
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this MethodInfo should be in a static lazy so we don't have to reflect in the hot path?

Copy link

Choose a reason for hiding this comment

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

Not caching reflection results is quite slow too. You can use static field to store typeof(AssemblyName).GetType().GetMethod("Clone") result.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. @AndyGerlicher if you do this, can you please do it for GetAssemblyLocation too? :)

MethodInfo method = assemblyNameToClone?.GetType().GetMethod("Clone");
result = (AssemblyName) method?.Invoke(assemblyNameToClone, null);
}
catch (MemberAccessException)
Copy link
Member

Choose a reason for hiding this comment

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

For that matter, catching here seems prone to slow.

@@ -39,5 +39,23 @@ public static Type GetTypeInfo(this Type t)
return t;
}
#endif

public static AssemblyName Clone2(this AssemblyName assemblyNameToClone)
Copy link
Contributor

Choose a reason for hiding this comment

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

CreateClone

catch (MemberAccessException)
{ }

return result ?? new AssemblyName(assemblyNameToClone.FullName);
Copy link
Contributor

Choose a reason for hiding this comment

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

You could look over Clone's implementation and copy it here? :)

@AndyGerlicher AndyGerlicher force-pushed the assemblynameclone branch 3 times, most recently from a2f9e21 to 7a63ca8 Compare April 27, 2017 20:24
@AndyGerlicher
Copy link
Contributor Author

@dotnet-bot test Windows_NT Build for CoreCLR please

/// </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

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).
@AndyGerlicher
Copy link
Contributor Author

@dotnet-bot test Windows_NT Build for CoreCLR please

@AndyGerlicher AndyGerlicher merged commit 5d14719 into dotnet:master Apr 27, 2017
AndyGerlicher added a commit that referenced this pull request May 1, 2017
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 #2015 (when running on .NET Core 2.0).
@AndyGerlicher AndyGerlicher deleted the assemblynameclone branch October 8, 2017 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants