Roslyn CSharpSyntaxTree.ParseText add link System.Runtime

I copied this code from the Roslyn Pluralsight tutorial.

string code = "class Foo { }"; var tree = CSharpSyntaxTree.ParseText(code); var node = tree.GetRoot(); Console.WriteLine(node.ToString()); var ret = SyntaxFactory.ClassDeclaration("Foo"). WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(new[] { SyntaxFactory.MethodDeclaration(SyntaxFactory. PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "Bar" ).WithBody(SyntaxFactory.Block() ) })).NormalizeWhitespace(); Console.WriteLine(ret.ToString());` 

In debug mode, I see that the variables "node" and "ret" silently throw this error:

The "Object" type is defined in an assembly that is not referenced. You must add a reference to the assembly "System.Runtime, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a".

There is no problem with this in the Roslyn Plurarsight video β€œCreating Syntax Trees Using APIs".

How to bind a link to the CSharpSyntaxTree class.

Edit:
I am using Visual Studio 2015 Preview.

Error in my Visual Studio 2015 Preview

There is no error in this simple code in the Pluralsight video.

Pluralsight video

Edit:

The only problem is the Visual Studio tooltip. I can use the syntax tree API without any problems. For example, this code records nodes and tokens without any strange exceptions, but Visual Studio will not show me the syntax tree in the tooltip when debugging the code.

 static void PrintSyntaxTree(SyntaxNode node) { if (node != null) { foreach (var item in node.ChildTokens()) { Console.Write(item); } Console.WriteLine(""); foreach (SyntaxNode child in node.ChildNodes()) PrintSyntaxTree(child); } } static void Main(string[] args) { string code = "class Foo { public void Bar(){} }"; var tree = CSharpSyntaxTree.ParseText(code); var node = tree.GetRoot(); foreach (SyntaxNode child in node.ChildNodes()) { PrintSyntaxTree(child); } } 
+6
source share

Source: https://habr.com/ru/post/980093/


All Articles