我尝试使用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版本,不可能尝试所有组合。