[英]Properties, methods, lambda expressions used in the code
I have a code: 我有一个代码:
using System.Linq.Expressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Expression<del> myET = x => x.Age; //for example in ASP.NET MVC forms
Person.Name = "vitia"
Person.Move();
}
}
}
I would like to know how to use Roslyn.NET can "pull" and write to the console all the methods, properties, and lambda used in the code. 我想知道如何使用Roslyn.NET可以“拉”并向控制台写入代码中使用的所有方法,属性和lambda。 Now I'm sitting on this for a few good hours and I can not think of anything. 现在我坐在这几个小时,我什么都想不到。 I tried to use the MemberAccesExpressionSyntax here but I do not really it came out. 我试图在这里使用MemberAccesExpressionSyntax,但我并没有真的出来。 Can you show some examples of doing something like that? 你能举一些这样做的例子吗? With this code, the screen would put the console: 使用此代码,屏幕将放置控制台:
x.Age Name Move x.Age Name Move
This is the sort of query you are looking for: 这是您正在寻找的那种查询:
var expressionNodes = syntaxTree.
GetRoot().
DescendantNodes().Where(n => n.Kind == SyntaxKind.[YourSyntaxKind]);
Below is the full sample of code. 以下是完整的代码示例。 You ought to be able to paste it into your C# console app. 您应该能够将其粘贴到C#控制台应用程序中。
I've pulled out: 我退出了:
I'll leave as an excercise for yourself to pull out the rest of what you need. 我将为自己留下一个练习,以取出你需要的其余部分。
private static void StackOverflowTest()
{
var syntaxTree = SyntaxTree.ParseText(@"
using System.Linq.Expressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Expression<del> myET = x => x.Age; //for example in ASP.NET MVC forms
Person.Name = ""vitia""
Person.Move();
}
}
}");
EmitStatement(syntaxTree, SyntaxKind.ParenthesizedLambdaExpression);
EmitStatement(syntaxTree, SyntaxKind.SimpleLambdaExpression);
EmitStatement(syntaxTree, SyntaxKind.MethodDeclaration);
}
private static void EmitStatement(SyntaxTree syntaxTree, SyntaxKind sk)
{
var expressionNodes = syntaxTree.
GetRoot().
DescendantNodes().Where(n => n.Kind == sk);
foreach (var expressionNode in expressionNodes)
{
Console.WriteLine(expressionNode.ToString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.