In all examples when using Roslyn, you have something like this:
SyntaxTree tree = CSharpSyntaxTree.ParseText(
@"using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld
{
// A whole program here...
}");
var root = (CompilationUnitSyntax)tree.GetRoot();
// Getting the semantic model (for MSCORELIB)
var compilation = CSharpCompilation.Create("HelloWorld")
.AddReferences(
MetadataReference.CreateFromFile(
typeof(object).Assembly.Location))
.AddSyntaxTrees(tree);
var model = compilation.GetSemanticModel(tree);
How to get the semantic model for my code?
The last piece of code retrieves the semantic model for mscorelib types: MetadataReference.CreateFromFile(typeof(object).Assembly.Location) so that I can inspect usings or other parts of the source and get symbol information.
But if I define types in HelloWorld and wanted to retrieve symbol info from those, I would the semantic model. But since I just loaded mscorelib I would not get this info.
How to load the semantic model for the source I just defined?