Reflection on C# 3.5

Posted on November 20, 2007

C# 3.5

Expression<Func<int, bool>> filter = n => (n * 3) < 5; 

BinaryExpression lt = (BinaryExpression) filter.Body;
BinaryExpression mult = (BinaryExpression) lt.Left;           
ParameterExpression en = (ParameterExpression) mult.Left;           
ConstantExpression three = (ConstantExpression) mult.Right;
ConstantExpression five = (ConstantExpression) lt.Right;

"(LessThan (Multiply n 3) 5)" ==
      string.Format("({0} ({1} {2} {3}) {4})",
                     lt.NodeType, mult.NodeType,

                     en.Name, three.Value, five.Value);

Maybe in C# 4.0 instead of pretending to be functional we will try to be and writing code like this: 

(define (factorial n)
    (let ((product 1)
          (counter 1))
    (define iter)
       (if (> counter n)
           product
          (begin (set! product (* counter product))
                 (set! counter (+ counter 1))
          (iter))))
     (iter)))

And in C# 5.0 we may finally figure how to use pieces of alien technology:

(define (factorial n)
   (define (iter product counter)
        (if (> counter n)
            product
           (iter (* counter product)
                 (+ counter 1))))
   (iter 1 1))

And after 5.0, maybe we will finally code like it was ment to be.

The point is, C# 3.5 is an interesting language and dig it!

Filed Under Uncategorized |

1 Comment so far
  1. while(availableTime>0) { July 9, 2008 5:47 pm

    Expression Tree + Reflection + C# 3.5…

    I’ve been trying to do something like this since C# 1.1. Tonight I got to do it: SomeCode.DoSomethingInProperty(cust…

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name

Email

Website

Comments

For spam detection purposes, please copy the number 8989 to the field below:

© Copyright 0xDEADBEEFCAFE • Powered by Wordpress • Design by Sebastin.

free web hit counter