<legend id='KCQVJ'><style id='KCQVJ'><dir id='KCQVJ'><q id='KCQVJ'></q></dir></style></legend>

<small id='KCQVJ'></small><noframes id='KCQVJ'>

<tfoot id='KCQVJ'></tfoot>
    <i id='KCQVJ'><tr id='KCQVJ'><dt id='KCQVJ'><q id='KCQVJ'><span id='KCQVJ'><b id='KCQVJ'><form id='KCQVJ'><ins id='KCQVJ'></ins><ul id='KCQVJ'></ul><sub id='KCQVJ'></sub></form><legend id='KCQVJ'></legend><bdo id='KCQVJ'><pre id='KCQVJ'><center id='KCQVJ'></center></pre></bdo></b><th id='KCQVJ'></th></span></q></dt></tr></i><div id='KCQVJ'><tfoot id='KCQVJ'></tfoot><dl id='KCQVJ'><fieldset id='KCQVJ'></fieldset></dl></div>

        <bdo id='KCQVJ'></bdo><ul id='KCQVJ'></ul>

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

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

        • <legend id='tfqbh'><style id='tfqbh'><dir id='tfqbh'><q id='tfqbh'></q></dir></style></legend>
          • <i id='tfqbh'><tr id='tfqbh'><dt id='tfqbh'><q id='tfqbh'><span id='tfqbh'><b id='tfqbh'><form id='tfqbh'><ins id='tfqbh'></ins><ul id='tfqbh'></ul><sub id='tfqbh'></sub></form><legend id='tfqbh'></legend><bdo id='tfqbh'><pre id='tfqbh'><center id='tfqbh'></center></pre></bdo></b><th id='tfqbh'></th></span></q></dt></tr></i><div id='tfqbh'><tfoot id='tfqbh'></tfoot><dl id='tfqbh'><fieldset id='tfqbh'></fieldset></dl></div>

              <small id='tfqbh'></small><noframes id='tfqbh'>

                <tbody id='tfqbh'></tbody>

              1. <tfoot id='tfqbh'></tfoot>
                • <bdo id='tfqbh'></bdo><ul id='tfqbh'></ul>
                  本文介绍了如何从 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;
                  }
                  

                  方法调用是这样的……

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

                  我们正在调用的方法现在在另一个类中,并且不是在此实例上调用,而是在名为 calledClassCalledClass 实例上调用.但是编译器如何将 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 表达式中获取引用实例的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  <legend id='RMyB8'><style id='RMyB8'><dir id='RMyB8'><q id='RMyB8'></q></dir></style></legend>

                  • <tfoot id='RMyB8'></tfoot>

                  • <i id='RMyB8'><tr id='RMyB8'><dt id='RMyB8'><q id='RMyB8'><span id='RMyB8'><b id='RMyB8'><form id='RMyB8'><ins id='RMyB8'></ins><ul id='RMyB8'></ul><sub id='RMyB8'></sub></form><legend id='RMyB8'></legend><bdo id='RMyB8'><pre id='RMyB8'><center id='RMyB8'></center></pre></bdo></b><th id='RMyB8'></th></span></q></dt></tr></i><div id='RMyB8'><tfoot id='RMyB8'></tfoot><dl id='RMyB8'><fieldset id='RMyB8'></fieldset></dl></div>

                          <bdo id='RMyB8'></bdo><ul id='RMyB8'></ul>
                              <tbody id='RMyB8'></tbody>

                            <small id='RMyB8'></small><noframes id='RMyB8'>