未定义或导入奇怪的诊断错误,预定义的类型为System ... [英] Strange diagnostics errors, prefedined type System... is not defined or imported

查看:74
本文介绍了未定义或导入奇怪的诊断错误,预定义的类型为System ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解析一个非常基本的.NET 4.6应用程序时,我遇到了Roslyn诊断错误.解决方案文件可以从那里下载

解决方案和项目构建良好,但Roslyn仍然给出这些错误.也许错误会误导我,并且我需要以某种方式配置项目?知道如何解决这些错误吗?

以下是用于解析文件的代码:

  var properties = new Dictionary< string,string>{["DesignTimeBuild"] ="true",["CheckForSystemRuntimeDependency"] ="true"};var工作空间= MSBuildWorkspace.Create(properties);workspace.WorkspaceFailed + =(发送方,args)=>{};Workspace.LoadMetadataForReferencedProjects = true;解决方案solution = Workspace.OpenSolutionAsync(SolutionFilePath).Result;foreach(解决方案中的var p.Projects){foreach(p.Documents中的var文件){var语义模型= file.GetSemanticModelAsync().Result;var mscorlib = MetadataReference.CreateFromFile(file.FilePath);var conversion = CSharpCompilation.Create("MyCompilation",new [] {语义模型.语法树},new [] {m​​scorlib});var model = Compilation.GetSemanticModel(semanticModel.SyntaxTree);varclarificationDiagnistics = model.Compilation.GetDeclarationDiagnostics(CancellationToken.None);var parseDiagnostics = model.Compilation.GetParseDiagnostics(CancellationToken.None);var allDiagnostics = model.Compilation.GetDiagnostics(CancellationToken.None);var methodBodyDiagnostics = model.Compilation.GetMethodBodyDiagnostics(CancellationToken.None);}} 

订阅workspace.workspaceFailed事件将导致以下错误:

使用以下命令处理文件"MYPATH \ BLL.csproj"时,Msbuild失败消息:C:\ Program Files(x86)\ Microsoft VisualStudio \ 2017 \ Enterprise \ MSBuild \ 15.0 \ Bin \ Microsoft.Common.CurrentVersion.targets:(1656,5):无法执行"GetReferenceNearestTargetFrameworkTask"任务从程序集"C:\ Program Files(x86)\ Microsoft实例化视觉的Studio \ 2017 \ Enterprise \ Common7 \ IDE \ CommonExtensions \ Microsoft \ NuGet \ NuGet.Build.Tasks.dll".请验证任务程序集是使用相同版本构建的Microsoft.Build.Framework程序集中的一个安装在您的计算机上计算机,并且您的主机应用程序没有缺少绑定重定向为Microsoft.Build.Framework.无法转换类型的对象键入'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask''Microsoft.Build.Framework.ITask'.C:\ Program Files(x86)\ Microsoft视觉的Studio \ 2017 \ Enterprise \ MSBuild \ 15.0 \ Bin \ Microsoft.Common.CurrentVersion.targets:(1656,5):已执行"GetReferenceNearestTargetFrameworkTask"任务声明或使用不正确,或在构建过程中失败.检查任务名称和程序集名称的拼写.

解决方案

所以这终于解决了:

  1. 添加了@GeorgeAlexandria建议的microsoft.build重定向 http://github.com/Microsoft/msbuild/issues/2369#issuecomment-353674937

  2. 从出纸槽文件夹中清除了Microsoft.Build.*

  3. 添加了Microsoft.Build.Locator作为参考

  4. 在工作区代码上方添加了MSBuildLocator.RegisterDefaults()行.

来源: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421

I am getting Roslyn diagnostics errors when parsing a very basic .NET 4.6 application. The solution files can be downloaded from there https://github.com/dotnet/roslyn/files/2393288/DemoSolution.zip

The dependency tree looks like this:

BLL -> DB

I am getting the following diagnostics errors in the BLL project:

The solution and projects build fine, still Roslyn gives these errors. Maybe the errors are misleading and I need to configure the projects in someway? Any idea how I can resolve these errors?

Here is the code used for parsing the files:

var properties = new Dictionary<string, string>
                {
                    ["DesignTimeBuild"] = "true",
                    ["CheckForSystemRuntimeDependency"] = "true"
                };
                var workspace = MSBuildWorkspace.Create(properties);

                workspace.WorkspaceFailed += (sender, args) =>
                {

                };
                workspace.LoadMetadataForReferencedProjects = true;

Solution solution = workspace.OpenSolutionAsync(SolutionFilePath).Result;               
                    foreach (var p in solution.Projects)
                    {   
                        foreach (var file in p.Documents)
                        {
      var semanticModel = file.GetSemanticModelAsync().Result;

                        var mscorlib = MetadataReference.CreateFromFile(file.FilePath);
                    var compilation = CSharpCompilation.Create("MyCompilation",
                        new[] { semanticModel.SyntaxTree }, new[] { mscorlib });

                    var model = compilation.GetSemanticModel(semanticModel.SyntaxTree);
                    var declarationDiagnistics = model.Compilation.GetDeclarationDiagnostics(CancellationToken.None);
                    var parseDiagnostics = model.Compilation.GetParseDiagnostics(CancellationToken.None);
                    var allDiagnostics = model.Compilation.GetDiagnostics(CancellationToken.None);
                    var methodBodyDiagnostics = model.Compilation.GetMethodBodyDiagnostics(CancellationToken.None);

                                  }
                }

Subscriping to the workspace.workspaceFailed event results in the following error:

Msbuild failed when processing the file 'MYPATH\BLL.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask' to type 'Microsoft.Build.Framework.ITask'. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

解决方案

So this finally solved it:

  1. Added the microsoft.build redirects as suggested by @GeorgeAlexandria http://github.com/Microsoft/msbuild/issues/2369#issuecomment-353674937

  2. Cleared the Microsoft.Build.* from the output bin folder

  3. Added the Microsoft.Build.Locator as a reference

  4. Added the line MSBuildLocator.RegisterDefaults() above the workspace code.

Source: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421

这篇关于未定义或导入奇怪的诊断错误,预定义的类型为System ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆