1. <tfoot id='yV6XV'></tfoot>
    2. <legend id='yV6XV'><style id='yV6XV'><dir id='yV6XV'><q id='yV6XV'></q></dir></style></legend>

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

    3. <small id='yV6XV'></small><noframes id='yV6XV'>

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

        表达式树和调用委托

        Expression Trees and Invoking a Delegate(表达式树和调用委托)
          <tbody id='ANwUP'></tbody>
          <tfoot id='ANwUP'></tfoot>
          • <bdo id='ANwUP'></bdo><ul id='ANwUP'></ul>

            1. <i id='ANwUP'><tr id='ANwUP'><dt id='ANwUP'><q id='ANwUP'><span id='ANwUP'><b id='ANwUP'><form id='ANwUP'><ins id='ANwUP'></ins><ul id='ANwUP'></ul><sub id='ANwUP'></sub></form><legend id='ANwUP'></legend><bdo id='ANwUP'><pre id='ANwUP'><center id='ANwUP'></center></pre></bdo></b><th id='ANwUP'></th></span></q></dt></tr></i><div id='ANwUP'><tfoot id='ANwUP'></tfoot><dl id='ANwUP'><fieldset id='ANwUP'></fieldset></dl></div>
            2. <small id='ANwUP'></small><noframes id='ANwUP'>

                1. <legend id='ANwUP'><style id='ANwUP'><dir id='ANwUP'><q id='ANwUP'></q></dir></style></legend>
                  本文介绍了表达式树和调用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我有一个 delegate 指向一些我在第一次创建 delegate 对象时实际上并不知道的函数.该对象稍后会设置为某个函数.

                  So I have a delegate which points to some function which I don't actually know about when I first create the delegate object. The object is set to some function later.

                  然后我还想创建一个表达式树,它使用参数调用委托(为了这个问题,参数可以是 5).这是我正在努力解决的问题;下面的代码显示了我想要的,但它没有编译.

                  I also then want to make an expression tree that invokes the delegate with an argument (for this question's sake the argument can be 5). This is the bit I'm struggling with; the code below shows what I want but it doesn't compile.

                  Func<int, int> func = null;
                  Expression expr = Expression.Invoke(func, Expression.Constant(5));
                  

                  对于这个例子我可以做(这很实用,因为我需要在运行时构建表达式树):

                  For this example I could do (this is practical since I need to build the expression trees at runtime):

                  Func<int, int> func = null;
                  Expression<Func<int>> expr = () => func(5);
                  

                  这使得 expr 变成:

                  () => Invoke(value(Test.Program+<>c__DisplayClass0).func, 5)
                  

                  这似乎意味着要使用 delegate func,我需要生成 value(Test.Program+<>c__DisplayClass0).func位.

                  Which seems to mean that to use the delegate func, I need to produce the value(Test.Program+<>c__DisplayClass0).func bit.

                  那么,如何创建一个调用委托的表达式树?

                  So, how can I make an expression tree which invokes a delegate?

                  推荐答案

                  好的,这显示了它是如何实现的(但在我看来它很不雅):

                  OK, this shows how it can be done (but it is very inelegant in my opinion):

                  Func<int, int> func = null;
                  Expression<Func<int, int>> bind = (x) => func(x);
                  
                  Expression expr = Expression.Invoke(bind, Expression.Constant(5));
                  
                  Expression<Func<int>> lambda = Expression.Lambda<Func<int>>(expr);
                  Func<int> compiled = lambda.Compile();
                  
                  Console.WriteLine(expr);
                  
                  func = x => 3 * x;
                  Console.WriteLine(compiled());
                  
                  func = x => 7 * x;
                  Console.WriteLine(compiled());
                  
                  Console.Read();
                  

                  基本上我使用 (x) =>func(x); 来创建一个调用委托所指向的函数.但是您可以看到 expr 过于复杂.出于这个原因,我不认为这个答案很好,但也许可以建立在它的基础上?

                  Essentially I use (x) => func(x); to make a function that calls what the delegate points to. But you can see that expr is overly complicated. For this reason I don't consider this answer good, but maybe it can be built upon?

                  这篇关于表达式树和调用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                      <bdo id='z7tN1'></bdo><ul id='z7tN1'></ul>

                      <legend id='z7tN1'><style id='z7tN1'><dir id='z7tN1'><q id='z7tN1'></q></dir></style></legend>
                          <tfoot id='z7tN1'></tfoot>
                            <tbody id='z7tN1'></tbody>

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

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