Looking for source code for System.Console nuget package


#1

I am very curious how some platform specific version of .NET Standard works.

For example if I use:
System.Console.WriteLine("hello world");

In a .NET Standard 1.6 library. And try to reference it in a .NET4.7 app, it will miss System.Console.dll at runtime. Which I guess enable .NET Standart on the standard .NET Framework. And I am very curious how this work.
However I have been unlucky with my googling and didn’t find the source for that (just the Console.cs in the full framewok, which is not what I am after)

Anyone know where I could find System.Console.dll source code?


#2

If you look at the System.Console package (I use NuGet Package Explorer for that), you will notice that it contains the lib/net46/System.Console.dll file. That’s what is going to decide how the .Net Standard version of System.Console.WriteLine is going to run on .Net Framework 4.6+.

And if you look at that file in a decompiler (I use dotPeek), you will see:

[assembly: TypeForwardedTo(typeof (Console))]

If you Ctrl + Q on Console, you will see that it comes from mscorlib.dll.

Another option is to use ildasm, which shows the same information as:

.class extern forwarder System.Console
{
  .assembly extern mscorlib
}

So, in the end, .Net Standard System.Console is run using the normal mscrolib System.Console on .Net Framework. And if you’re looking for the source of that, it’s on Reference Source.


#3

Mmm… I am still confused on how I would replicate that.
I have a piece of code which compile on .NET core, .NET Framework, Xamarin for iOS, Android and PCL.
But it doesn’t compiler for .NET Standard 1.6 because it contains DBNull, which seems to be missing.

Should I do like I do with PCL?

For PCL , my PCL library is only used at compile time. At runtime I have an identical library for each targeted runtime (same assembly name and classes inside) but I used TypeForward attribute to all existing type, such as DBNull, whereas the PCL implementation (that I provide) of such classes if full of NotImplementedException()


.NET Foundation Website | Blog | Projects | Code of Conduct