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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:53:17+00:00 2026-06-04T15:53:17+00:00

I would like to get both the compiled Func(of boolean) and an Expression( of

  • 0

I would like to get both the compiled Func(of boolean) and an
Expression( of Func(of boolean)) using a single parameter. I don’t intend to modify
the expression tree. The only reason to take an Expression tree at all
is so i can print out the code that is being executed.

void Assert(Expression<Func<bool>> assertionExpression) {
    if (!assertionExpression.Compile()()) 
    { 
        throw new AssertFailedException(assertionExpression.ToString()); 
    }
}

Is there any reasonable way to do this?

As a corrolarry, in the case of simple compiler generated expression trees, will the same instance always be passed as a parameter?

static Dictionary<Expression<Func<bool>>, Func<bool>> cache;

static void Assert(Expression<Func<bool>> assertionExpression) {
    Func<bool> method = null;
    if (!cache.TryGetValue(assertionExpression, out method)) {
        cache.Add(assertionExpression, method = assertionExpression.Compile());
        Console.WriteLine("cache insert");
    }
    else {
         Console.WriteLine("cache hit");
    }
    if (!method()) 
    { 
        throw new AssertFailedException(assertionExpression.ToString()); 
    }
}

static void someCodeThatExecutesRegularly() {
    Assert(()=>true);
}

public static void Main(string[] args, int argc) 
{
    someCodeThatExecutesRegularly();

    someCodeThatExecutesRegularly();

    someCodeThatExecutesRegularly();
}

Will the output be ‘cache insert, ‘cache hit’, ‘cache hit’ or ‘cache insert’, ‘cache insert’, ‘cache insert’.

  • 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-06-04T15:53:19+00:00Added an answer on June 4, 2026 at 3:53 pm

    For the first question, you can take the expression tree and compile it to get an executable version, although it will recompile each time so you would have to cache it.

    static void Assert(Expression<Func<bool>> assertionExpression) {
        var func = assertionExpression.Compile(); // if you call func() it will execute the expression
    }
    

    For your second question, it will be a new expression tree each time, so you will get “cache insert” “cache insert” “cache insert” etc…

    To get it working with the cache and compiling you could possibly use .ToString() on the expression to get the string representation of the function, but this could cause very confusing issues if you have any closures because the string representation could be the same but the closed variables would be different, so if you do do this, USE WITH CARE!

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

Sidebar

Related Questions

How would I get both param1 and param2 to my controller, I tried like
I would like to get the final SQL query, with parameter values bound (replaced)
I would like to get the dimensions (coordinates) for all the HTML elements of
I would like to get the most out of working in cch or tcsh
I would like to get a reference to all objects in the Java heap,
I would like to get some information on list of points that needs to
I would like to get contents of some stream or project I can find
I would like to get the AssemblyCompany attribute from a WinForm project inside of
I would like to get the XAML source of a WPF Window (MainWindow). Clicking
I would like to get the first item from a list matching a condition.

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.