我试图通过Roslyn向我的项目添加程序集引用。我有什么
static void UpdateProjects(string soluiton, string assemblyPath)
{
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(soluiton).Result;
var rewrittenSolution = solution;
var projectIds = rewrittenSolution.ProjectIds;
for (int i = 0; i < projectIds.Count; i++)
{
var addedRef = MetadataReference.CreateFromFile(assemblyPath)
.WithProperties(new MetadataReferenceProperties(MetadataImageKind.Assembly));
rewrittenSolution = rewrittenSolution.AddMetadataReference(projectIds[i], addedRef);
workspace.TryApplyChanges(rewrittenSolution);
}
}
它不起作用。当我执行TryApplyChanges时,Roslyn中出现错误的System.NullReferenceException。 我做错了什么?
Stack Trace:
System.NullReferenceException
at Microsoft.CodeAnalysis.MSBuild.ProjectFile.AddMetadataReference(MetadataReference reference, AssemblyIdentity identity)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.ApplyMetadataReferenceAdded(ProjectId projectId, MetadataReference metadataReference)
at Microsoft.CodeAnalysis.Workspace.ApplyProjectChanges(ProjectChanges projectChanges)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.ApplyProjectChanges(ProjectChanges projectChanges)
at Microsoft.CodeAnalysis.Workspace.TryApplyChanges(Solution newSolution, IProgressTracker progressTracker)
at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.TryApplyChanges(Solution newSolution, IProgressTracker progressTracker)
at RoslynTestApp.Program.UpdateProjects(String soluiton, String assemblyPath)
at RoslynTestApp.Program.Main(String[] args)
谢谢!