以下是完整的代码:
MSBuildWorkspace msBuild = MSBuildWorkspace.Create();
Solution sln = await msBuild.OpenSolutionAsync(solutionPath);//NullReferenceException
其中solutionPath
是有效的 .sln Visual Studio 2015解决方案。我尝试了一个Winforms新项目和一个简单类的dll。我在尝试阅读之前关闭了该解决方案,但这没有帮助。它仍然抛出NullReferenceException。
我正在使用VS2015。 x64 Windows 10。
MSBuildWorkspace
Workspace
是错误的吗?还缺少其他东西吗?
(NullReferenceException实际上是一个由方法抛出的奇怪错误。这通常是像mistakenNull.DoSomething();.
这样的错误类型。编辑2 - 此代码位于解决方案的dll项目中,该解决方案还包含一个调用此代码的简单Winforms项目。 (因为Roslyn项目模板创建了一个dll项目。)也许这很重要。
<小时/>
异常:对象引用未设置为对象的实例。
堆栈跟踪:
at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.GetReferences(CSharpCompilerInputs compilerInputs, ProjectInstance executedProject, IEnumerable`1& metadataReferences, IEnumerable`1& analyzerReferences)
at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.CreateProjectFileInfo(CSharpCompilerInputs compilerInputs, ProjectInstance executedProject)
at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.<GetProjectFileInfoAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<LoadProjectAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<GetOrLoadProjectAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenSolutionAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at namespace1.RosTest.<RenameMethods>d__0.MoveNext() in C:\Users\...
答案 0 :(得分:1)
您需要确保在申请旁边加{4}}或Microsoft.CodeAnalysis.CSharp.Workspaces.dll
。
工作区代码动态加载它们。
答案 1 :(得分:0)
我遇到了同样的问题,当我安装了正确的版本时,这个问题得到了解决 &#34; Microsoft.CodeAnalysis.CSharp&#34;从以下链接汇编。
https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp/
这可能对某人有帮助,所以我在这里发帖。