c# - C# - roslyn代码分析从错误解决方案返回false构建错误

我尝试使用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预览中没有错误,

我的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:UtilsCA5CA5.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框架有引用。

对于.NET 4.8的目标,使用C# 8.0 (最新的Roslyn推荐),


var net48Path ="C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.8";


var usedLibs = new List<string>() {


"mscorlib.dll",


"System.Core.dll",


"System.Data.dll",


"System.Data.DataSetExtensions.dll",


"System.Xml.dll",


"System.Xml.Linq.dll",


"System.Numerics.dll",


"Microsoft.CSharp.dll",


"System.Net.dll",


"System.Web.dll",


"System.ComponentModel.DataAnnotations.dll",


"System.ComponentModel.Composition.dll",


"System.Drawing.dll"


};


var parameters = new CompilerParameters(usedLibs.ToArray()) {


 GenerateInMemory = true,


 CompilerOptions = $"/langversion:8.0 /lib:"{net48Path}""


};


var compileResults = CsCodeProvider.CompileAssemblyFromSource(parameters, ... ...



...