C# Roslyn的编译失败,但可以使用Visual Studio

C# Roslyn的编译失败,但可以使用Visual Studio,c#,roslyn,C#,Roslyn,我正在尝试加载一个现有的C#项目,并使用Roslyn分析它的一个源文件。但是,在编译源文件时,会出现编译错误,这些错误毫无意义,并且在使用Visual Studio编译时不存在。(代码也可以正常运行) 有人能帮我诊断这个问题吗 用于编译文件的部分代码: var oriSln = workspace.OpenSolutionAsync(@"absolutepath.sln").Result; var oriProject = oriSln.Projects.First(p => p.Name

我正在尝试加载一个现有的C#项目,并使用Roslyn分析它的一个源文件。但是,在编译源文件时,会出现编译错误,这些错误毫无意义,并且在使用Visual Studio编译时不存在。(代码也可以正常运行)

有人能帮我诊断这个问题吗

用于编译文件的部分代码:

var oriSln = workspace.OpenSolutionAsync(@"absolutepath.sln").Result;
var oriProject = oriSln.Projects.First(p => p.Name == "projectname");
var doc = oriProject.GetDocument(docId);
var model = doc.GetSemanticModelAsync().Result;
var diag = model.GetDiagnostics(); // has strange errors
错误:

thefile.cs(36,68): error CS0518: Predefined type 'System.Void' is not defined or imported
thefile.cs(40,74): error CS0518: Predefined type 'System.Void' is not defined or imported
thefile.cs(40,50): error CS1503: Argument 1: cannot convert from 'float' to 'int'
thefile.cs(41,50): error CS1503: Argument 1: cannot convert from 'float' to 'int'
thefile.cs(51,50): error CS1503: Argument 2: cannot convert from 'float' to 'int'
thefile.cs(58,58): error CS1503: Argument 2: cannot convert from 'float' to 'int'
thefile.cs(130,40): error CS0518: Predefined type 'System.ValueType' is not defined or imported
thefile.cs(168,55): error CS0246: The type or namespace name 'Vector3' could not be found (are you missing a using directive or an assembly reference?)
thefile.cs(158,38): warning CS0168: The variable 'ex' is declared but never used
thefile.cs(7,1): hidden CS8019: Unnecessary using directive.
thefile.cs(6,1): hidden CS8019: Unnecessary using directive.

该解决方案由大约30个项目组成,目标项目引用了其中的一些项目,还有一些第三方,其中一个包含
Vector3

我找到了这个问题的解决方案。我从解决方案中引用的另一个项目没有加载任何MetadataReferences,因此缺少mscrolib。我认为这会导致依赖项目抛出这些错误


至于它为什么不加载MetadataReferences,我不完全确定它为什么不加载任何MetadataReferences。我发现csproj文件有一个默认配置Debug | AnyCPU,该配置在本项目中不存在,因此我将其更改为Debug | x86。然后它正确加载了所有引用,我前面提到的编译错误现在都消失了。

相关吗?我不这么认为,我没有使用ASP.Net或DNVM,因此我无法将解决方案映射到我的问题。这可能与加载错误版本的mscorlib是同一个问题,但就我所见,它指的是32位v4.0框架,因为我认为它应该是配置问题,如果还没有,请随时提交一个bug。