0

After investing ~8 hours on endless search around the google, I have to ask you guys here on the community. Pls help :-)

This is theoretical Q, and I want to understand how this "magic" implemented by Microsoft .Net Core CLR. While I understand how to implement my own code to be able to work on different OS (by using Interface and P/Invoke of specific platform) from the answer: https://stackoverflow.com/a/52297584/10696080

I don't understand how the .Net Core CLR DLLs implements the same functionality. For "RuntimeInformation.OSDescription" or "RuntimeInformation.IsOSPlatform" for example.

By looking on the source code of .Net Core RuntimeInformation class it looks like is not implemented by Interface and P/Invorke , but by some mysterious method.

My findings till here:

  1. I find more general question: .Net Core Multi-Platform underlying framework implementation But is not answers my Q.

  2. The microsoft site also not explains this

  3. Decompiling System.Runtime.InteropServices.RuntimeInformation.dll reveal only empty static properties

Question: Pls advise where can I look for the explanation of how the .Net Core CLR DLLs implements multi-platform support (ot more specifically, how RuntimeInformation.OSDescription works)

andreyk2 Hohlov
  • 619
  • 2
  • 9
  • 10
  • 1
    For RuntimeInformation specifically, the code is [here](https://github.com/dotnet/runtime/tree/3ea278dae3906826fe85a032a45e04d9c1975d94/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src). This DLL has platform specific versions, i.e. it is compiled separately for each target platform. The only magic is compiler flags that include different code in the platform specific versions. I suspect your decompilation was of a [reference assembly](https://learn.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies) that only contains metadata for public members. – Mike Zboray Oct 05 '20 at 16:25
  • @MikeZboray, thanks! It make sense! So, if RuntimeInformation.dll has some platform specific versions, how CLR know to load the right version? I mean where this info is stored? in the internal code of CLR? – andreyk2 Hohlov Oct 05 '20 at 18:24
  • 1
    Assuming you are talking about an application, at some point there is platform dependent code running on the machine. Either you have published a framework dependent version of the app, which uses the version installed on the target machine (i.e. RuntimeInformation.dll is there on the machine) or you using a self-contained version which includes the platform dependent components in the application folder (the correct version of RuntimeInformation is included along-side your app). If you're only shipping a DLL, references are included in the metadata which will be resolved by downstream apps. – Mike Zboray Oct 06 '20 at 04:14
  • @MikeZboray, I got it. HUGE thanks. Yes, I meant to the 1-rst option (framework dependent version of the app, which uses the version installed on the target machine) – andreyk2 Hohlov Oct 06 '20 at 14:05

1 Answers1

1

.Net Core CLR DLLs implements multi-platform support implemented by:

  1. .NET Core source code has target specific OS preprocessor directives:

    *#if TARGET_WINDOWS ...

    #elif TARGET_OSX ...

    #elif TARGET_UNIX ...*

  2. Conditional compilation is done and DLL compiled separately for each target platform. So there are some versions of .NET Core DLLs (in the .NET Core SDK)

  3. While deploying the app:

    A. Fremework dependent deployment. Your app uses the OS specific DLL version, which already installed on the target machine (in .NET Core runtime)

    B. Self contained deployment. Platform specific DLL taken from SDK and included in the application folder

andreyk2 Hohlov
  • 619
  • 2
  • 9
  • 10