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 99837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:27:52+00:00 2026-05-11T00:27:52+00:00

I have an expression tree I have created by parsing an Xml using the

  • 0

I have an expression tree I have created by parsing an Xml using the expression class in C#. See this question.

I only have Add, Subtract, Divide, Multiply, Parameters, And and Or in my Expression Tree. Is there a way to convert this ExpressionTree into a callable method? …or do I have to emit the IL manually?

Kind regards,

  • 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. 2026-05-11T00:27:53+00:00Added an answer on May 11, 2026 at 12:27 am

    Here’s an example of both approaches. If I have missed something, or you want more information, just let me know.

    static void Main() {     // try to do 'x + (3 * x)'      var single = BuildSingle<decimal>();     var composite = BuildComposite<decimal>();      Console.WriteLine('{0} vs {1}', single(13.2M), composite(13.2M)); } // utility method to get the 3 as the correct type, since there is not always a 'int x T' static Expression ConvertConstant<TSource, TDestination>(TSource value) {     return Expression.Convert(Expression.Constant(value, typeof(TSource)), typeof(TDestination)); } // option 1: a single expression tree; this is the most efficient static Func<T,T> BuildSingle<T>() {             var param = Expression.Parameter(typeof(T), 'x');     Expression body = Expression.Add(param, Expression.Multiply(         ConvertConstant<int, T>(3), param));     var lambda = Expression.Lambda<Func<T, T>>(body, param);     return lambda.Compile(); } // option 2: nested expression trees: static Func<T, T> BuildComposite<T>() {      // step 1: do the multiply:     var paramInner = Expression.Parameter(typeof(T), 'inner');     Expression bodyInner = Expression.Multiply(         ConvertConstant<int, T>(3), paramInner);     var lambdaInner = Expression.Lambda(bodyInner, paramInner);      // step 2: do the add, invoking the existing tree     var paramOuter = Expression.Parameter(typeof(T), 'outer');     Expression bodyOuter = Expression.Add(paramOuter, Expression.Invoke(lambdaInner, paramOuter));     var lambdaOuter = Expression.Lambda<Func<T, T>>(bodyOuter, paramOuter);      return lambdaOuter.Compile(); } 

    Personally, I would aim towards the first method; it it both simpler and more efficient. This might involve passing the original parameter throughout a stack of nested code, but so be it. I have got some code somewhere that takes the ‘Invoke’ approach (composite), and re-writes the tree as the first approach (single) – but it is quite complex and long. But very useful for Entity Framework (which doesn’t support Expression.Invoke).

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Updated: first you need to handle the load event of… May 11, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer We had the same issue with a web service that… May 11, 2026 at 4:40 pm
  • Editorial Team
    Editorial Team added an answer Add a 'Being Processed' Flag to each record, which is… May 11, 2026 at 4:40 pm

Related Questions

I am trying to create an expression tree that represents the following: myObject.childObjectCollection.Any(i =>
I am just getting started with expression trees so I hope this makes sense.
I know Linq-to-SQL is dead, but anyway, I think this is really basic, I'm
I have a Task object that has a collection of Label objects ... in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.