System.Reflection not being referenced from nuget package folder

Viewed 338

I am installing System.Reflection nuget package into my project but when I check the reference path to the dll, it shows C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\Facades\System.Reflection.dll

I have even tried removing it and adding it manually from the nuget packages folder but it still shows the same path. How do I make sure that it is referenced from the nuget packages folder and not from program files?

1 Answers

The System.Reflection package contains almost nothing, but empty folders.

System.Reflection package content

In the lib folder there is only one System.Reflection.dll for .NET Framework 4.6.2, but even that assembly is empty except for a reference to mscorlib. Consequently, except for .NET Framework 4.6.2, there will not be any assembly in the NuGet package folder or your project output directory. These assemblies are provided by the underlying framework runtime.

The path that you see is the path to the reference assembly. That is also what the package supplies in the ref folders. For the difference between reference and runtime assemblies, see this related post. To quote @hansPassant and @MartinUllrich:

So as Hans Passant already mentioned, "reference" assemblies are used to build programs, which means that this is the assembly that is passed to the compiler as a reference. At runtime however, the implementation might be different. Apart from the framework itself, this may be used by any NuGet package that distributes a single compile-time reference assembly but a variety of implementation assemblies for each target (.NET Core, .NET Framework, MonoAndroid etc.). The lib folder in NuGet packages can even be used to add more private implementation assemblies that it doesn't want consuming applications to reference directly.

Reference assemblies only have "stub" methods so that the available API surface is defined and can be inspected by the compiler.

Related