Is it possible to convert a Roslyn AST expression tree to a LINQ expression Tree? Is there a Roslyn ScriptEngine.Evaluate?
Monday, November 14, 2011 2:19 AM
I'd like to use Roslyn to get the Expression that is assigned to an argument in an custom attribute and evaluate it to get the runtime value. I'd replace all enumerations with their integer equivalent first so that all operands are primitives. I figured Roslyn would expose some API to evaluate an expression or convert it to an LINQ expression tree but I cannot locate it. So does Roslyn expose an API to convert a Roslyn AST to a Linq.Expression or straight to a delegate? I see ScriptEngine.Execute which takes in a string and executes the code. But what if I have a Roslyn AST (say LiteralExpressionSyntax) and I want to evaluate that?
Monday, November 14, 2011 8:19 PMOwner
As far as I am aware, there are no APIs that can take a Roslyn AST and return the corresponding LINQ expression tree.
You could use the scripting APIs to evaluate simple expressions as shown in the below example - but this wouldn't work for more complicated cases. You can also use semanticInfo.ConstantValue to evaluate expressiosn that only contain compile time constants as shown in the below example.
Update: Unfortunately, GetSemanticInfo() does not seem to work when you pass it the AttributeSyntax.Expression. This looks like a bug to me - however, since full support for Attributes is not done yet, this is probably expected at the moment.
This thread also has some related discussion - http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/5ce1b129-92fa-4867-9df2-509154ef4c9c
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
Tuesday, November 15, 2011 7:36 PMOwner
See this post for more explanation of LINQ and Expr Trees v2: http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/6313ffe8-db52-4e93-9578-f1af06b03823
The short answer is that we may provide, or someone may write a Roslyn tree to ET v2, but Roslyn trees can represent the full languages of VB and C# while ETs v2 cannot (for example, type definitions or some ref-involved exprs).
Shyam pointed you to a couple of examples of the Roslyn Scripting API, and there are two walkthroughs in the CTP that demonstrate them. You can also see the doc for where our current thinking is, but these APIs are more likely than not to change: http://tinyurl.com/3ezykda .
If you're curious, the full ET spec is at http://tinyurl.com/ylkf2ke . There are several pages of design intro that discusses some trade-offs and limitations. You'll note that any converter needs to map Roslyn Syntax API plus info from the Roslyn Semantic API to ETs since ETS are a semantic model, not syntactic.
- Edited by billchi_msOwner Tuesday, November 15, 2011 7:42 PM