如何从一个lambda表达式得到了下文称实例的实例 [英] How to get the instance of a reffered instance from a lambda expression

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

问题描述

我有这样的lambda表达式表达式来; Func键<布尔>> 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?

我要执行这个类的一些addiontal方法或得到一些属性值。

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



这里的问题是,中投的常量表达式的结果为。因此,常量表达式总是空。

任何想法?

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



但在这里谈到一个新的问题。我只得到窗口形式,其中我的供应商的下文称实例所在的实例。

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

我如何获得实物(< lambda表达式code> ACLASS )?

推荐答案

这实际上是可能的,但它取决于你传递到这个方法是什么。假设你有,你传递的类的实例方法,你是在为 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;
}



更​​复杂的情况是如下:

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

我们正在调用该方法现在是在另一个类不叫在这种情况下,但是, calledClass 的一个实例名为 calledClass 。但是,编译器如何通过 calledClass 可变进lambda表达式?没有什么定义字段 calledClass 该方法 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.

编译器通过产生与一个字段的内部类名为 calledClass 解决这个问题。其结果是 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天全站免登陆