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

Related Questions

I have this regular expression: ^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$ however it is failing when i have an
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
How would I go about using an Expression Tree to dynamically create a predicate
I'm trying to create an expression tree dynamicly. Let asume that I have two
I have two expression trees defined like this: private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get;
This may be familiar to some. I have a wrapper class Ex that wraps
I have an string/expression like this: (((p1 == 1) && (p2 != 2)) ||
I have a regular expression to match a persons name. So far I have
I have the same expression in Javascript but it won't work in PHP for
Does anyone have a regular expression handy that will match any legal DNS hostname

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.