views:

91

answers:

2

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?

A: 

It is not possible "out of the box", you may be able to hack something with reflection, but that is not advisable, it will be very backwards.
Edit: Actually possible according to Ronald, but still quite backwards. Hidden side effects like this make the code hard to read and maintain.

Instead your ProcessCommand should take either the whole aClass object or more preferably an IMyCommand interface with .Method() and the additional methods and properties that ProcessCommand needs. Then the aClass.GetType() type should implement IMyCommand.

Albin Sunnanbo
+2  A: 

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.

Ronald Wildenberg
awesome :) thanks!
Rookian
Could you mark this as the answer please if it answers your question?
Ronald Wildenberg
I have edited my question ... please review
Rookian
It turns out to be a little more complicated than I first thought. I tested with too simple an example. I'll update my answer to show how it's possible after all.
Ronald Wildenberg
thanks, this works fine :)
Rookian