如何从Lambda表达式中获取引用实例的实例 [英] How to get the instance of a referred instance from a lambda expression

查看:81
本文介绍了如何从Lambda表达式中获取引用实例的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个lambda表达式 Expression< Func< bool>> commandToExecute

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())

如何在 ProcessCommand 方法中获取 aClass 的实例?

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.

这可能吗?

编辑:
我现在编写了一个简单的静态助手方法来获取实例:

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);

这里的问题是,对ConstantExpression的强制转换为 Null 。因此, constantExpression 始终为空。

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

有任何想法吗?

编辑2
我解决了问题...

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;
}

但这是一个新问题。我只得到提供者的引用实例所在的Windows窗体的实例。

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

如何获取真实对象 aClass )的lambda表达式?

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

推荐答案

这实际上是可能的,但是它取决于您传递给此方法的内容。假设有一种情况,您将您所在类的实例方法传递给 ProcessCommand

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) { ... }
}

然后,您可以使用以下 ProcessCommand 方法。这仅是因为在此实例上调用了 MethodToCall

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;
}

更复杂的情况如下:

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; }
}

我们正在调用的方法现在在另一个类中,没有被调用在此实例上,但在 CalledClass 称为 CalledClass 的实例上。但是,编译器如何将 namedClass 变量传递给lambda表达式?没有定义字段 Class 的字段,可以调用方法 MethodToCall

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.

编译器通过生成一个内部类来解决此问题,该内部类的一个字段名为称为Class 。结果, ProcessCommand 方法现在变为:

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.

这篇关于如何从Lambda表达式中获取引用实例的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆