Replacement for unavailable Assembly methods
Hi,
How is the supported way to deal with the Assembly class? For instance, I have some method calls like Assembly.Location or Assembly.GetCallingAssembly() which I couldn't find a way to bring those calls to CoreFX.
Thanks.
/cc @terrajobst @nguerrera
Assembly.GetCallingAssembly() has very bad semantics in the face of inlining: see remarks in https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly(v=vs.110).aspx. If you're using this for logging/diagnostics, consider the much cheaper caller information attributes: https://msdn.microsoft.com/en-us/library/hh534540.aspx. If you're using this to make code-path decisions, I'd urge you to reconsider based on the poorly defined semantics of GetCallingAssembly.
Assembly.Location is problematic for two reasons: not all assemblies (consider loaded from byte[]) have locations and on some platforms (.NET Native), none do. What is your use case for Assembly.Location?
@jkotas @gkhanna79 how would you feel about adding Assembly.TryGetLocation()? It just keeps coming up and it seems a lot of libraries rely on being able to find other files that are meant to be placed alongside their dlls. (Just guessing for now that is the use-case here too.) There is AppContext.BaseDirectory, but that is not necessarily the library directory, and I think for DNX it never is.
not all assemblies (consider loaded from byte[]) have locations
Assembly.Location deals with this case just fine - by returning empty string.
Assembly.TryGetLocation()
I do not have a problem with it. The method name and signature are different, but it would otherwise do the exact same thing as Assembly.Location on CoreCLR - correct?
Assembly.TryGetLocation()
We may want to let AssemblyLoadContext override what it returns ... I guess DNX may need it to handle the in-memory compilation, etc.
I do not have a problem with it. The signature is different, but it would otherwise do the exact same thing as Assembly.Location on CoreCLR - correct?
Yes. I actually meant bool TryGetLocation(out string location), just to draw more attention to the fact that you have to deal with it not being available, and opening up the possibility of putting it elsewhere than core reflection as an extension.
However, on second thought, exposing Assembly.Location as-is would be much simpler and .NET Native could simply return String.Empty. Maybe that is best. What do you think?
We may want to let AssemblyLoadContext override what it returns ... I guess DNX may need it to handle the in-memory compilation, etc.
Hmm, I'm not sure about what would be a good override if there's no .dll on disk at all. cc @davidfowl, @steveharter.
I think it would be better if the host-overridable API returned the containing directory and not the full path to a .dll. And the path of last resort could be AppContext.BaseDirectory.
Something like:
public static string GetBaseDirectory(this Assembly assembly)
{
#if HAVE_ASSEMBLY_LOAD_CONTEXT
string dir = AssemblyLoadContext.GetLoadContext(assembly).GetBaseDirectory(assembly);
if (!String.IsNullOrEmpty(dir))
return dir;
#endif
#if HAVE_ASSEMBLY_LOCATION
string location = assembly.Location;
if (!String.IsNullOrEmpty(location))
return Path.GetDirectoryName(location);
#endif
return AppContext.BaseDirectory;
}
I think that could handle 99% of the use cases I've seen (i.e. "give me the best place to find files related to this assembly") with a fully portable API.
I would prefer we just add back Assembly.Location as opposed to adding the TryGetLocation.
Closing this as fixed by dotnet/corefx#3861, which adds Location. We're firmly decided against GetCallingAssembly().
@nguerrera I'm trying to get a byte array from a loaded assembly, so I can send it over the wire and use types from it in a remote process. What would be the current supported way of doing this?