问题描述
我有这个 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;
}
方法调用是这样的……
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.
如何获取 lambda 表达式的真实对象(aClass
)?
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
实例上调用.但是编译器如何将 calledClass
变量传递给 lambda 表达式呢?没有定义可以调用方法 MethodToCall
的字段 callClass
.
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 表达式中获取引用实例的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!