Skip to content

Commit

Permalink
Merge pull request #41180 from sharwell/rm-reflection
Browse files Browse the repository at this point in the history
Use public APIs instead of reflection
  • Loading branch information
sharwell committed Jan 23, 2020
2 parents f6d1934 + 71e7c35 commit 6cb854a
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions src/Compilers/Core/MSBuildTask/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,41 +152,15 @@ internal static Exception GetLocalizedArgumentException(string errorString,

internal static string TryGetAssemblyPath(Assembly assembly)
{
if ((bool?)typeof(Assembly).GetTypeInfo()
.GetDeclaredProperty("GlobalAssemblyCache")
?.GetMethod.Invoke(assembly, parameters: null) == true)
if (assembly.GlobalAssemblyCache)
{
return null;
}

var codebase = (string)typeof(Assembly)
.GetTypeInfo()
.GetDeclaredProperty("CodeBase")
?.GetMethod.Invoke(assembly, parameters: null);

if (codebase != null)
if (assembly.CodeBase is { } codebase)
{
var uri = new Uri(codebase);
if (uri.IsFile)
{
return uri.LocalPath;
}
else
{
var callingAssembly = (Assembly)typeof(Assembly)
.GetTypeInfo()
.GetDeclaredMethod("GetCallingAssembly")
?.Invoke(null, null);

var location = (string)typeof(Assembly).GetTypeInfo()
.GetDeclaredProperty("Location")
?.GetMethod.Invoke(assembly, parameters: null);

if (location != null)
{
return location;
}
}
return uri.IsFile ? uri.LocalPath : assembly.Location;
}

return null;
Expand Down

0 comments on commit 6cb854a

Please sign in to comment.