【发布时间】:2017-04-08 10:12:00
【问题描述】:
在使用Roslyn 的所有示例中,您都有这样的情况:
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);
如何获取我的代码的语义模型?
最后一段代码检索mscorelib 类型的语义模型:MetadataReference.CreateFromFile(typeof(object).Assembly.Location),以便我可以检查usings 或源的其他部分并获取符号信息。
但如果我在 HelloWorld 中定义类型并想从中检索符号信息,我会使用语义模型。但是因为我刚刚加载了mscorelib,所以我不会得到这个信息。
如何为我刚刚定义的源加载语义模型?
【问题讨论】: