How can I find the platform a .NET assembly has be

2019-07-13 11:53发布

问题:

Looking at the following JetBrains dotPeek view of some given assemblies, how can I programmatically determine the platform such assemblies have been compiled against?

I have tried the following method which does not work for assemblies compiled in .NET Core.

public static string GetFrameworkVersion(Assembly assembly)
{
    var targetFrameAttribute = assembly.GetCustomAttributes(true)
        .OfType<TargetFrameworkAttribute>().FirstOrDefault();
    if (targetFrameAttribute == null)
    {
        return ".NET 2, 3 or 3.5";
    }

    return targetFrameAttribute.FrameworkDisplayName.Replace(".NET Framework", ".NET");
}

How is dotPeek able to obtain such details?