solution.GetDocument(syntaxTree)
gives you the document that gave you that syntax tree when you called GetSyntaxTreeAsync
. You can't pass it an arbitrary syntax tree -- only one you got from a document somewhere. It's just a convenient helper to "go back" to the document from whence it came.
I'm not exactly sure what you're trying to do from the snippet, so I'm going to make three guesses:
if you're trying to analyze a file that's already in that solution, you should be taking the Solution object you got in
originalSolution
and find the file document there.if you want to do analysis as if that additional tree was also added to your document, you can call
Solution.AddDocument()
to add that as a document, and then you can analyze it from there. Remember Roslyn is immutable, when you callSolution.AddDocument
, you get a new solution to analyze, so hold onto what it gives you!If all you're trying to do is find a symbol which you already know by name, consider finding the Project in your solution that contains that type, calling
GetCompilationAsync
, and then either callingGetTypeByMetadataName
or walking the namespaces to get your type symbol.