Skip to content

Instantly share code, notes, and snippets.

@npnelson
Last active August 27, 2017 01:15
Show Gist options
  • Save npnelson/5bf49a145338120cacbac7ee8bd399bf to your computer and use it in GitHub Desktop.
Save npnelson/5bf49a145338120cacbac7ee8bd399bf to your computer and use it in GitHub Desktop.
GetCoreCLRVersion
public static class CoreClrHelpers
{
static string coreCLRVersion = "NOT_YET_ASSESSED";
public static string GetCoreClrVersion()
{
if (coreCLRVersion == "NOT_YET_ASSESSED") //the following code might take some time to run, but we only need to do the heavy lifting once. Not sure if this is the best way to determine CLr version, but it works
{
var appDomainType = typeof(object).GetTypeInfo().Assembly?.GetType("System.AppDomain");
var currentDomain = appDomainType?.GetProperty("CurrentDomain")?.GetValue(null);
var deps = appDomainType?.GetMethod("GetData")?.Invoke(currentDomain, new[] { "FX_DEPS_FILE" });
if (deps == null)
{
coreCLRVersion = "";
return coreCLRVersion;
}
coreCLRVersion = GetCoreClrVersionImpl(deps.ToString());
}
return coreCLRVersion;
}
internal static string GetCoreClrVersionImpl(string deps)
{
var result = Regex.Match(deps, "(?:(\\d+)\\.)?(?:(\\d+)\\.)?(?:(\\d+)\\.\\d+)").Value;
return result;
}
}
@beeradmoore
Copy link

Tried this to get the dotnet runtime version on AWS Lambda, deps is null so returns empty string. Running locally here I get correct output (1.0.4, which is what I installed). Unsure if the difference is linux vs macOS or if they are using a different version and it breaks in that specific version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment