-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
src/Shared/AssemblyNameExtension.cs
Outdated
#else | ||
newExtension.asAssemblyName = new AssemblyName(asAssemblyName.FullName); | ||
#endif | ||
newExtension.asAssemblyName = asAssemblyName.Clone2(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CloneIfPossible
?
src/Shared/AssemblyUtilities.cs
Outdated
AssemblyName result = null; | ||
try | ||
{ | ||
MethodInfo method = assemblyNameToClone?.GetType().GetMethod("Clone"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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? :)
src/Shared/AssemblyUtilities.cs
Outdated
MethodInfo method = assemblyNameToClone?.GetType().GetMethod("Clone"); | ||
result = (AssemblyName) method?.Invoke(assemblyNameToClone, null); | ||
} | ||
catch (MemberAccessException) |
There was a problem hiding this comment.
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.
src/Shared/AssemblyUtilities.cs
Outdated
@@ -39,5 +39,23 @@ public static Type GetTypeInfo(this Type t) | |||
return t; | |||
} | |||
#endif | |||
|
|||
public static AssemblyName Clone2(this AssemblyName assemblyNameToClone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreateClone
src/Shared/AssemblyUtilities.cs
Outdated
catch (MemberAccessException) | ||
{ } | ||
|
||
return result ?? new AssemblyName(assemblyNameToClone.FullName); |
There was a problem hiding this comment.
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? :)
a2f9e21
to
7a63ca8
Compare
@dotnet-bot test Windows_NT Build for CoreCLR please |
/// </summary> | ||
private static void Initialize() | ||
{ | ||
if (s_initialized) return; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
7a63ca8
to
99340d4
Compare
@dotnet-bot test Windows_NT Build for CoreCLR please |
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).
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).