Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:17:50+00:00 2026-05-16T08:17:50+00:00

This may be familiar to some. I have a wrapper class Ex that wraps

  • 0

This may be familiar to some. I have a wrapper class Ex that wraps an expression tree with a bunch of implicit conversions and operators. Here is simplified version

public class Ex 
{
    Expression expr;

    public Ex(Expression expr)
    {
        this.expr = expr;
    }
    public static implicit operator Expression(Ex rhs) { return rhs.expr; }
    public static implicit operator Ex(double value) 
    { return new Ex(Expression.Constant(value, typeof(double))); }
    public static implicit operator Ex(string x) 
    { return new Ex(Expression.Parameter(typeof(double), x)); }
    public static Ex operator +(Ex left, Ex right)
    {
        return new Ex(Expression.Add(left, right));
    }
    public static Ex operator -(Ex rhs)
    {
        return new Ex(Expression.Negate(rhs));
    }
    public static Ex operator -(Ex left, Ex right)
    {
        return new Ex(Expression.Subtract(left, right));
    }
    public static Ex operator *(Ex left, Ex right)
    {
        return new Ex(Expression.Multiply(left, right));
    }
    public static Ex operator /(Ex left, Ex right)
    {
        return new Ex(Expression.Divide(left, right));
    }
}

So here is what I want to do:

{ ...
    Ex x = "x";
    Ex y = 10.0;
    Ex z = x + y;

    LambdaExpression lambda = BuildLambda(z);
    Func<double,double> f = (Func<double,double>)lambda.Compile();

    // f(5) = 15

}

But how to I transverse the tree propely and build my lambda’s (or delegates)

    LambdaExpression BuildLambda(Expression e)
    {
        ConstantExpression cex = e as ConstantExpression;
        if(cex != null)
        {
            return Expression.Lambda<Func<double>>( cex );
        }
        ParameterExpression pex = e as ParameterExpression;
        if (pex != null)
        {
            Func<Expression, Expression> f = (x) => x;
            Expression body = f(pex);
            return Expression.Lambda<Func<double, double>>( body , pex);
        }
        BinaryExpression bex = e as BinaryExpression;
        if (bex != null)
        {
            LambdaExpression left = GetLambda(bex.Left);
            LambdaExpression rght = GetLambda(bex.Right);
   // Now what?
        }
        return null;
    }

I have tried several things to get to convert the BinaryExpression bex into a lambda, and all have been unsucessful up to now. I’d like some suggestions and pointers. Note that the operands of the operation might be other expression objects and only at the leafs of the tree they will either be ParameterExpression or ConstantExpression.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:17:51+00:00Added an answer on May 16, 2026 at 8:17 am

    You can create the Expression Tree as you call the conversion operators:

    public class Ex
    {
        private readonly Expression expr;
    
        public Ex(Expression expr)
        {
            this.expr= expr;
        }
    
        public Expression Expression
        {
            get { return this.expr; }
        }
    
        public static Ex operator +(Ex left, Ex right)
        {
            return new Ex(Expression.Add(left.expr, right.expr));
        }                                       ↑           ↑
    
        // etc.
    }
    

    At each step, you “unpack” the Expression from the Ex instance(s), apply the Expression.* method, and wrap the result in a new Ex instance.

    At the end, all you have to do is extract the Expression from the final Ex instance:

    Ex x = new Ex(Expression.Parameter(typeof(double), "x"));
    Ex y = new Ex(Expression.Constant(10.0, typeof(double)));
    Ex z = x + y;
    
    Expression<Func<double, double>> result =
        Expression.Lambda<Func<double, double>>(z.Expression, x.Expression);
    

    Note that the C# compiler provides the feature to create an Expression Tree for you:

    Expression<Func<double, double>> result = x => x + 10.0;
    

    creates exactly the same Expression Tree as the code above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm aware some of you may not be familiar with Monotouch, but this could
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the
This may sound like a very generic question but here it goes. I have
I have some strange behavior occurring in an ASP.NET application that I am trying
I have been pondering about this for some time. How do you pick a
I'm familiar with the DOMDocument::importNode method for importing a tree of nodes from some
This may seem like a very basic question, since I'm not familiar with jQuery,
I ask here in the hope that some members will be familiar enough with
Have seen some conversations revolving around this, but hoping for some current input as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.