How to compile a c# Lambda expression to a System.Linq.Expression.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Han

    #1

    How to compile a c# Lambda expression to a System.Linq.Expression.

    Hi, all,
    If I write follow code in c# method. The IDE will compile it to a complex construct method of System.Linq.Exp ression.
    Expression<Func <int>ex = () =10;
    will be compile to:
    Expression<Func <int>ex = Expression.Lamb da<Func<int>(Ex pression.Litera l(10))
    It is so smart. I can travel in this expression tree. I can do some smart abilities on this design.

    Now, I want write a console application. If user input follow string from console,
    () =10
    I want make a LambdaExpressio n object and execute it in my context.
    I can use CodeDom to compile some c# code to a dynamic assembly. But I have not found a utility to compile c# code to an expression object.

    How to compile a c# Lambda expression to a Expression object? Help me. Thanks.

  • Marc Gravell

    #2
    Re: How to compile a c# Lambda expression to a System.Linq.Exp ression.

    Well, it isn't that simple... you'd need to parse that yourself and
    build the Expression by hand, or use the "dynamic LINQ" sample (which
    does much of that):



    An alternative would be to use the CSharpCodeProvi der to compile it *as*
    C# (with suitable wrappers), and load the generated assembly on the fly;
    not pretty.

    Marc

    Comment

    • Colin Han

      #3
      Re: How to compile a c# Lambda expression to a System.Linq.Exp ression.

      Thanks Marc.

      I had download the sample. It look is so cool. Thank you vary much.

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:%23$MTa11L JHA.4536@TK2MSF TNGP03.phx.gbl. ..
      Well, it isn't that simple... you'd need to parse that yourself and
      build the Expression by hand, or use the "dynamic LINQ" sample (which
      does much of that):
      >

      >
      An alternative would be to use the CSharpCodeProvi der to compile it *as*
      C# (with suitable wrappers), and load the generated assembly on the fly;
      not pretty.
      >
      Marc

      Comment

      Working...