简体   繁体   English

没有工作区的Roslyn解析语法树

[英]Roslyn Resolved Syntax Tree without Workspaces

I am trying to use Roslyn to create a C# code editor. 我正在尝试使用Roslyn创建C#代码编辑器。 I would like to get a resolved syntax tree so that I can add code completion support. 我想获得一个解析的语法树,以便可以添加代码完成支持。

However, I cannot use Workspaces since they are broken on iOS. 但是,我无法使用工作区,因为它们在iOS上已损坏。

I am therefore trying to hit the C# compiler directly using Compilations (is this right?). 因此,我试图直接使用Compilations打C#编译器(对吗?)。 The API is simple enough, but I cannot figure out how to get member information for types. 该API非常简单,但是我无法弄清楚如何获取类型的成员信息。

Specifically, I would like to see a Members property of the class C that includes a reference to the fully declared D class. 具体来说,我希望看到C类的Members属性,其中包括对完全声明的D类的引用。

However, the INamedTypeSymbol doesn't have any useful info attached to it. 但是, INamedTypeSymbol没有附加任何有用的信息。

How do I get rich type info from a CSharpCompilation? 如何从CSharpCompilation获取丰富的类型信息?

var cst = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit (
    "public class C { public D foo; }");
var dst = Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseCompilationUnit (
    "public class D { public C bar; }");

var comp = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.
    Create ("Poo").
    AddSyntaxTrees (cst.SyntaxTree).
    AddSyntaxTrees (dst.SyntaxTree);

var classC = comp.GetSymbolsWithName (n => n == "C").FirstOrDefault (); //= C
var classD = comp.GetSymbolsWithName (n => n == "D").FirstOrDefault (); //= D

var otherClassC = comp.GetTypeByMetadataName ("C"); //= C

Console.WriteLine ("C = " + classC);

您正在寻找GetMembers()方法 ,该方法返回该类型中所有已声明的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM