0

I'm building a .NET tool that needs to query project properties like Target Framework/s. My first thought has been Microsoft.CodeAnalysis.

using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;

public static class Program
{
    public static async Task Main()
    {
        MSBuildLocator.RegisterDefaults(); 
        using var workspace = MSBuildWorkspace.Create();
        var pr = await workspace.OpenProjectAsync("E:\\Repos\\SuperJMN\\DotNet-Ssh-Deployer\\NetCoreSsh\\DotNetSsh.csproj");
    }
}

I've loaded a project, and I can see a lot of information in the pr instance, but I haven't found anything related to the TFMs.

SuperJMN
  • 13,110
  • 16
  • 86
  • 185

1 Answers1

0

You can leverage reflection's Assembly.ImageRuntimeVersion property to grab it.

Here's the documentation on it: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.imageruntimeversion?redirectedfrom=MSDN&view=net-6.0#System_Reflection_Assembly_ImageRuntimeVersion

Chobo
  • 76
  • 8
  • Thanks! But, if I'm not wrong, that's only applicable to assemblies that are already generated. My scenario should cover cases when the project hasn't been compiled yet. – SuperJMN Jan 16 '22 at 22:24
  • Interesting question! Maybe you can use `Microsoft.Build`'s `GetPropertyValue(PropertyNames.TargetFramework)` for this? – Chobo Jan 16 '22 at 22:51
  • Thanks :) Unfortunately, I haven't found the correct way to invoke the GetPropertyValue, my `pr` reference is of type `Microsoft.CodeAnalysis.Project`. Am I using the correct approach? – SuperJMN Jan 17 '22 at 16:05