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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:25:38+00:00 2026-05-16T16:25:38+00:00

I have this lambda expression Expression<Func<bool>> commandToExecute Then I pass an instance of a

  • 0

I have this lambda expression Expression<Func<bool>> commandToExecute

Then I pass an instance of a class in there with a method:

_commandExecuter.ProcessCommand (() => aClass.Method())

How do I get the instance of aClass within the ProcessCommand method?

I want to execute some addiontal methods of this class or get some property values.

Is this possible?

EDIT:
I now have written a simple static helper method to get the instance:

private static object GetReferredProviderInstance(Expression body)
{
    var methodCallExpression = body as MethodCallExpression;
    if (methodCallExpression != null)
    {
        var constantExpression = methodCallExpression.Object as ConstantExpression;
        if (constantExpression != null) return constantExpression.Value;
    }
    return null;
}

The method call looks like this …

Expression body = commandToExecute.Body; // this is the method parameter Expression<Func<bool>> commandToExecute
var referredProviderInstance = GetReferredProviderInstance(body);

The problem here is, that the cast to the ConstantExpression results into Null. So the constantExpression is always null.

Any ideas?

EDIT 2
I fixed the problem …

private static object GetReferredProviderInstance(Expression body)
{
    var methodCallExpression = body as MethodCallExpression;
    if (methodCallExpression != null)
    {
        var memberExpression = methodCallExpression.Object as MemberExpression;
        if (memberExpression != null)
        {
            var constantExpression = memberExpression.Expression as ConstantExpression;
            if (constantExpression != null) return constantExpression.Value;
        }
    }
    return null;
}

But here comes a new problem. I only get the instance of the windows form where the reffered instance of my provider is located.

How do I get the real object (aClass) of the lambda expression?

  • 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-16T16:25:39+00:00Added an answer on May 16, 2026 at 4:25 pm

    This is actually possible but it depends on what you pass into this method. Suppose you have the scenario where you pass an instance method of the class that you are in to ProcessCommand:

    public class TestClass
    {
        public void TestMethod()
        {
            ProcessCommand(() => MethodToCall());
        }
        public bool MethodToCall() { return true; }
        void ProcessCommand(Expression<Func<bool>> expression) { ... }
    }
    

    Then you can use the following ProcessCommand method. This only works because MethodToCall is called on this instance.

    void ProcessCommand(Expression<Func<bool>> expression)
    {
        var lambda = (LambdaExpression) expression;
        var methodCall = (MethodCallExpression) lambda.Body;
        var constant = (ConstantExpression) methodCall.Object;
        var myObject = constant.Value;
    }
    

    The more complicated scenario is as follows:

    public class CallingClass
    {
        public void TestMethod()
        {
            var calledClass = new CalledClass();
            ProcessCommand(() => calledClass.MethodToCall());
        }
        void ProcessCommand(Expression<Func<bool>> expression) { ... }
    }
    public class CalledClass
    {
        public bool MethodToCall() { return true; }
    }
    

    The method we are calling is now in another class and isn’t called on this instance but on an instance of CalledClass called calledClass. But how does the compiler pass the calledClass variable into the lambda expression? There is nothing that defines a field calledClass that the method MethodToCall can be called on.

    The compiler solves this by generating an inner class with one field with the name calledClass. As a result the ProcessCommand method now becomes this:

    public void ProcessCommand(Expression<Func<bool>> expression)
    {
        // The expression is a lambda expression with a method call body.
        var lambda = (LambdaExpression) expression;
        var methodCall = (MethodCallExpression) lambda.Body;
        // The method is called on a member of some instance.
        var member = (MemberExpression) methodCall.Object;
        // The member expression contains an instance of the anonymous class that
        // defines the member...
        var constant = (ConstantExpression) member.Expression;
        var anonymousClassInstance = constant.Value;
        // ...and the member itself.
        var calledClassField = (FieldInfo) member.Member;
        // With an instance of the anonymous class and the field, we can get its value.
        var calledClass =
            (CalledClass) calledClassField.GetValue(anonymousClassInstance);
    }
    

    Slightly more complicated because the compiler has to generate an anonymous inner class.

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

Sidebar

Related Questions

I have this method: public static Expression<Func<MyEntity, bool>> MyMethod(string someId) { return o =>
I have a method which have this signature public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this
I have a this line of code: var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param); where
I have this expression : Expression<Func<string, bool>> f = s => s.Length < 5;
I have the following method I can pass in a lambda expression to filter
I have this code : public static Expression<Func<T, bool>> CreatePredicate<T>(string typeSearch, string searchField, string
I am very confused. I have this lambda expression: tvPatientPrecriptionsEntities.Sort((p1, p2) => p1.MedicationStartDate .Value
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do
I have a method Get on a type MyType1 accepting a Func<MyType2, bool> 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.