您好,我正在尝试使用 Roslyn 分析我的 CsProj。我在寻找解决方案时遇到问题。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;
namespace Test
{
class Programm
{
static async Task Main(string[] args)
{
SourceAnalyzer sa = new SourceAnalyzer();
string path = @"C:\Users\lhafer\source\repos\Farm\Farm\Farm.csproj";
using (var workspace = MSBuildWorkspace.Create())
{
var project = await workspace.OpenProjectAsync(path);
var compilation = await project.GetCompilationAsync();
}
//sa.Analyze(path).Wait();
}
}
}
例外说:
System.Reflection.ReflectionTypeLoadException: "Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
谢谢您的帮助!
在尝试创建工作区之前,您需要
MSBuildLocator.RegisterDefaults();
。它位于 Microsoft.Build.Locator
nuget 包中。