【发布时间】:2019-11-04 17:17:15
【问题描述】:
我尝试使用 Roslyn 来分析一个非常简单的 C# 解决方案,一个带有简单骨架程序的控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CA5
{
class Program
{
static void Main(string[] args)
{
}
}
}
上面的代码在 VS 2019 Preview 中构建没有任何错误。 对于这样一个简单的解决方案/程序,我希望 Roslyn CA 能够正常工作,但 Roslyn 编译返回错误,主要与无法识别的类型有关。
我的 CA 代码是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
namespace CA5X
{
class Program
{
static void Main(string[] args)
{
const string path = @"C:\Utils\CA5\CA5.sln";
var props = new Dictionary<string, string>();
props["CheckForSystemRuntimeDependency"] = "true";
MSBuildWorkspace buildWorkspace = MSBuildWorkspace.Create(props);
Solution solutionToAnalyze = buildWorkspace.OpenSolutionAsync(path).Result;
foreach (Microsoft.CodeAnalysis.Project sProject in solutionToAnalyze.Projects)
{
Compilation sCompilation = sProject.GetCompilationAsync().Result;
var errors = sCompilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error);
Console.WriteLine("COUNT: " + errors.Count().ToString());
foreach (Microsoft.CodeAnalysis.Diagnostic diagnostic in errors)
{
Console.WriteLine(diagnostic.GetMessage());
}
}
}
}
}
编译后检索到的诊断信息如下:
The type or namespace name 'AssemblyTitleAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTitle' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyDescriptionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyDescription' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyConfigurationAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyConfiguration' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCompanyAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCompany' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyProductAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyProduct' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCopyrightAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCopyright' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTrademarkAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTrademark' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCultureAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCulture' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'ComVisibleAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'ComVisible' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'GuidAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Guid' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyVersion' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyFileVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyFileVersion' could not be found (are you missing a using directive or an assembly reference?)
Predefined type 'System.String' is not defined or imported
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
Predefined type 'System.Object' is not defined or imported
Predefined type 'System.String' is not defined or imported
Predefined type 'System.Void' is not defined or imported
'object' does not contain a constructor that takes 0 arguments
我只能猜测这是因为一些配置问题,但是有这么多 .NET 版本和这么多 VS 版本是不可能尝试所有组合的。
【问题讨论】:
标签: c# .net roslyn roslyn-code-analysis