Runtime: How can I detect my code is running on top of .net core?

Created on 15 Jul 2017  路  9Comments  路  Source: dotnet/runtime

    public class PlatformUtilities
    {
        public static bool IsRunningOnMono
        {
            get
            {
                return Type.GetType("Mono.Runtime") != null; // I need something like this.
            }
        }

        public static bool IsRunningOnDotNetCore
        {
            get
            {
                // What should I return here?
            }
        }
    }

Thanks in advance.

area-System.Runtime question

Most helpful comment

I think you can use System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription API, which is part of .NET Standard 1.1+ and .NET Core 1.0+, also see source code.

Check out IsNetCore implementation from our test sources.

All 9 comments

The option that comes to mind is to check for the System.Runtime.Loader.AssemblyLoadContext type, which exists in .Net Core, but not in .Net Framework:

c# public static bool IsRunningOnDotNetCore => Type.GetType("System.Runtime.Loader.AssemblyLoadContext") != null;

Though I don't know if there are any guarantees this will work the same in the future.

I think you can use System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription API, which is part of .NET Standard 1.1+ and .NET Core 1.0+, also see source code.

Check out IsNetCore implementation from our test sources.

@ysmoradi Please reopen if the solution above is insufficient. For a better way you may want to see/contribute to https://github.com/dotnet/corefx/issues/17452

@karelz's IsNetCore link is now dead due to the file having moved here. A more permanent link could be this (tag based link).

@fredericDelaporte thanks! Link updated in my post above to avoid further confusion. Thanks!

I have a need for this but I can't introduce a new dependency in the project to get System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.

Is there anything I could reach into inside something like System.Private.CoreLib/mscorlib to differentiate Core from Framework?

@rjmholt why cannot you introduce new ref-assembly dependency? That is THE official API and how it should be referenced.
Anything else will be brittle and unsupported.

@rjmholt https://github.com/dotnet/corefx/issues/22293#issuecomment-315527735 should at least tell you whether you are not on.NET Framework. Amother option get the path to the assembly in which typeof(string) is found and look at the name.
As @karelz says, brittle.

Thanks for the help @karelz and @danmosemsft. For some more context, I've got a C# project currently targeted to net46 whose assembly I'm trying to be able to run from PowerShell Core. I can change the C# code, but due to project requirements I can't add new dependencies. I may be able to retarget the .NET Framework version, so I'm going to investigate that. My ideal would be to use the correct API, but I'm just aware I likely need a fallback.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GitAntoinee picture GitAntoinee  路  3Comments

matty-hall picture matty-hall  路  3Comments

btecu picture btecu  路  3Comments

aggieben picture aggieben  路  3Comments

EgorBo picture EgorBo  路  3Comments