如何从 lambda 免费vpn 表达式中获取引用实例的实例

4

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

问题描述

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

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

然后我用一个方法传递一个类的实例:

Then I pass an instance of vpn下载 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 免费vpn 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)
{
    vpn下载 var methodCallExpression = 免费vpn body as MethodCallExpression;
    if (methodCallExpression != null)
    {
        var constantExpression = methodCallExpression.Object as ConstantExpression;
        免费vpn if (constantExpression != null) return constantExpression.Value;
    免费vpn下载 free vpn }
    return free vpn null;
}

方法调用是这样的……

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

这里的问题是,对 ConstantExpression 的强制转换会导致 Null.所以 constantExpression 始终为空.

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

有什么想法吗?

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

EDIT 2 I fixed the problem ...

private static object GetReferredProviderInstance(Expression body)
{
    var methodCallExpression = body as MethodCallExpression;
    if (methodCallExpression != null)
    {
  免费vpn       var vpn下载 memberExpression = methodCallExpression.Object as free vpn MemberExpression;
  免费vpn       if vpn free (memberExpression != null)
        {
       免费vpn      var constantExpression = 免费vpn memberExpression.Expression as free vpn 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 免费vpn is located.

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

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

推荐答案

这实际上是可能的,但这取决于你传递给这个方法的内容.假设您有一个场景,您将所在类的实例方法传递给 ProcessCommand:

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

public vpn下载 class TestClass
{
 vpn free    public void TestMethod()
    {
        免费vpn下载 ProcessCommand(() => MethodToCall());
 vpn下载   免费vpn  }
    public bool free 免费vpn下载 vpn vpn free 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>> vpn下载 expression)
{
    var lambda = (LambdaExpression) expression;
  free vpn   var methodCall = (MethodCallExpression) lambda.Body;
    var constant = (ConstantExpression) methodCall.Object;
    免费vpn 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
{
   免费vpn下载 免费vpn  public bool MethodToCall() { return true; }
}

我们正在调用的方法现在在另一个类中,并且不是在此实例上调用,而是在名为 calledClassCalledClass 实例上调用.但是编译器如何将 calledClass 变量传递给 lambda 表达式呢?没有定义可以调用方法 MethodToCall 的字段 callClass.

The method we are calling is now in 免费vpn下载 another class vpn下载 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 免费vpn下载 the method MethodToCall can be called on.

编译器通过生成一个内部类来解决这个问题,该内部类具有一个名为 calledClass 的字段.结果,ProcessCommand 方法现在变成了这样:

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

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

稍微复杂一点,因为编译器必须生成一个匿名内部类.

Slightly more complicated because the compiler has to generate an anonymous 免费vpn inner vpn free class.

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast vpn下载 delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
vpn free 2023-11-11 免费vpn下载 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with free vpn Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work vpn下载 (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() vpn free 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14