dotnet / dotnet/dotnet-api-docs
Assembly.LoadFrom treats parameter as path rather than URI
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
## Description
`Assembly.LoadFrom` is supposed to take a URI as its parameter:
> The `assemblyFile` parameter must refer to a URI without escape characters. This method supplies escape characters for all invalid characters in the URI.
([docs.microsoft.com](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom))
On .NET Framework, you can pass in a `file://` URI (constructed from the `Assembly.CodeBase` URI of another assembly, for example) to `Assembly.LoadFrom` and the assembly will be loaded.
On .NET Core, the `assemblyFile` parameter is treated as a path. The URI gets passed into `Path.GetFullPath` which mangles the URI to begin with and continues to treat it as a path from there on. This never works for loading an assembly from any URI.
https://github.com/dotnet/runtime/blob/a92f4f02dcbbd76cb9edf682424d001b9eb6d870/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs#L329-L334
For `file://` URIs, the behavior of .NET Core should match the behavior of .NET Framework, which is also the documented behavior. I don't have any opinions about whether this should work for non-`file://` URIs.
## Test Case
```c#
System.Reflection.Assembly.LoadFrom(typeof(object).Assembly.CodeBase)
````
On .NET Framework, this returns `[mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]`. On .NET Core, this throws `FileNotFoundException` with a message like `Could not load file or assembly 'C:\YourProject\bin\Debug\netcoreapp2.1\file:\C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.19\System.Private.CoreLib.dll'. The filename, directory name, or volume label syntax is incorrect.`.
Contributor guide
Assessment
This issue has not been assessed yet.