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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:34:06+00:00 2026-05-10T22:34:06+00:00

I’m trying to learn about Expression trees, and I’ve created a method that takes

  • 0

I’m trying to learn about Expression trees, and I’ve created a method that takes an

Expression<Func<bool>>  

and executes it if it satisfies some conditions – see the code below.

        private static void TryCommand(Expression<Func<bool>> expression)         {             var methodCallExpression = expression.Body as MethodCallExpression;             if (methodCallExpression == null)             {                 throw new ArgumentException('expression must be a MethodCallExpression.');             }              if (methodCallExpression.Object.Type != typeof (MyClass))             {                 throw new ArgumentException('expression must be operating on an instanceof MyClass.');                             }              var func = expression.Compile();             var success = func.Invoke();              if(!success)             {                 Console.WriteLine(methodCallExpression.Method.Name + '() failed with error code ' + (func.Target as MyClass).GetError());             }         } 

The problem that

(func.Target as MyClass)  

is null. Clearly I’m doing something wrong! How do I access the instance that the method is operating on?

  • 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-10T22:34:06+00:00Added an answer on May 10, 2026 at 10:34 pm

    The target of the method call is an instance of MyClass, but the delegate itself isn’t the method call. It’s something which will perform the method call when it’s executed.

    If you look at func.Target, you’ll see it’s a System.Runtime.CompilerServices.ExecutionScope.

    Now you could test for that, cast to it, and then fetch either the Locals or the Globals (not sure which) to get the target. However, I suspect it would be cleaner just to change to use a Func<int> (or whatever type your error code is) and return the error code when you execute the delegate in the first place. Then you wouldn’t even need an expression tree.

    EDIT: Given your comments, I’d suggest:

    public static void TryCommand(Expression<Func<MyClass,bool>> command,                               MyClass c) {     // Code as before to find the method name etc.      Func<MyClass, bool> compiled = command.Compile();      if (!compiled(c))     {         Console.WriteLine(methodCallExpression.Method.Name             + '() failed with error code ' + c.GetError());     } } 

    You’d then call it with:

    TryCommand(x => x.SomeMethod(), myClass); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • 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 None of the other answers worked out for me so… May 11, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer This may be of some help. You are adding a… May 11, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer Well, you can do it today (vs 2010 beta). Is… May 11, 2026 at 9:58 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.