PC Review


Reply
Thread Tools Rate Thread

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

 
 
Colin Han
Guest
Posts: n/a
 
      16th Oct 2008
Hi, all,
If I write follow code in c# method. The IDE will compile it to a complex construct method of System.Linq.Expression.
Expression<Func<int>> ex = () => 10;
will be compile to:
Expression<Func<int>> ex = Expression.Lambda<Func<int>> (Expression.Literal(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 LambdaExpression 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.

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      16th Oct 2008
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):

http://weblogs.asp.net/scottgu/archi...y-library.aspx

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

Marc
 
Reply With Quote
 
Colin Han
Guest
Posts: n/a
 
      16th Oct 2008
Thanks Marc.

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

"Marc Gravell" <(E-Mail Removed)> wrote in message
news:%23$(E-Mail Removed)...
> 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):
>
> http://weblogs.asp.net/scottgu/archi...y-library.aspx
>
> An alternative would be to use the CSharpCodeProvider to compile it *as*
> C# (with suitable wrappers), and load the generated assembly on the fly;
> not pretty.
>
> Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need some more help with a linq query or lambda expression Tony Johansson Microsoft C# .NET 2 23rd Jan 2009 08:01 AM
need help to write a linq query or a lambda expression Tony Johansson Microsoft C# .NET 2 22nd Jan 2009 10:28 AM
about linq, IEnumerable<T> and Lambda expression Tony Johansson Microsoft C# .NET 2 5th Jan 2009 11:40 AM
Lambda expression Peb Microsoft C# .NET 7 27th Dec 2008 11:48 PM
What Lambda expression corresponds to this LINQ query? raylopez99 Microsoft C# .NET 25 6th Oct 2008 12:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:08 PM.