KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
918 stars 118 forks source link

Equivalent for Expression Trees #50

Closed rikimaru0345 closed 3 years ago

rikimaru0345 commented 5 years ago

Hey @KirillOsenkov, first of all RoslynQuoter is really useful, thank you for making it!

However I'm currently looking for a way to do the same for expression trees. Basically taking c# source code and turning it into Expression.* calls. Like an "Expression Tree Quoter" :smile:

Google wasn't of any help and since you made this awesome tool, I figured I should just ask you if you're aware of any way that could be done; or maybe a project that already does this. :smile:

svick commented 5 years ago

Have you tried decompiling the result of what the C# compiler does?

For example here is the result of decompiling a simple Expression, which shows you how to build it yourself using calls to methods on Expression.

rikimaru0345 commented 5 years ago

Yes, but this isn't so much about getting it done at all (just to look at it), but rather about having an automated way (a library!) to do it. Like a string ConvertCSharpToExpressionCalls(string cSharpCode);, hope that makes sense.

svick commented 5 years ago

Could you explain why you want that?

Generally, the output of a quoter is useful for a human, because they can then use it to write some other code.

But if you want a library, that usually means you want to process the output by a program. But what kind of program prefers e.g. Expression.Lambda<Func<int>>(Expression.Constant(0, typeof(int))) when () => 0 can do the same thing?

zspitz commented 3 years ago

A little late to the party, but:

@rikimaru0345 I've written a library that allows you to do this, and a Visual Studio debugging visualizer that leverages the library to display this for an expression tree at runtime.

Multiple renderers

@svick Being able to take the source code and convert it the compiler equivalent calls to factory methods is useful, but not always an option; specifically, when I don't have the source code of an expression tree, but only the runtime object.

KirillOsenkov commented 3 years ago

Wow @zspitz this is awesome!

KirillOsenkov commented 3 years ago

I'll close this issue as this seems to be a great solution: https://github.com/KirillOsenkov/RoslynQuoter/issues/50#issuecomment-757342066

davidfowl commented 2 years ago

zomg