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

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

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

        • <bdo id='rQNRw'></bdo><ul id='rQNRw'></ul>
      1. 请解释 .NET 代表

        Please Explain .NET Delegates(请解释 .NET 代表)

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

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

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

                  本文介绍了请解释 .NET 代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我阅读了 MSDN 和 Stack Overflow.我了解操作代表的一般作用,但无论我做了多少示例,它都不会点击.一般来说,代表的想法也是如此.所以这是我的问题.当你有这样的功能时:

                  So I read MSDN and Stack Overflow. I understand what the Action Delegate does in general but it is not clicking no matter how many examples I do. In general, the same goes for the idea of delegates. So here is my question. When you have a function like this:

                  public GetCustomers(Action<IEnumerable<Customer>,Exception> callBack)
                  {
                  }
                  

                  这是什么,我应该传递给它什么?

                  What is this, and what should I pass to it?

                  推荐答案

                  它需要一个接受 IEnumerable 和 Exception 并返回 void 的函数.

                  it expects a function that takes IEnumerable and Exception and returns void.

                  void SendExceptionToCustomers(IEnumerable<Customer> customers, Exception ex) {
                     foreach(var customer in customers)
                        customer.SendMessage(ex.Message);
                  }
                  
                  GetCustomers(SendExceptionToCustomers);
                  

                  顺便说一句,GetCustomers 对这个函数来说似乎是一个糟糕的名字——它要求一个动作,所以它更像是 DoSomethingToCustomers

                  btw, GetCustomers seems like a terrible name for this function -- it's asking for an action, so its more like DoSomethingToCustomers

                  编辑回应评论

                  好吧 有道理,那么现在为什么还要费心使用 GetCustomer 函数呢?如果我只是将它重命名为 GetCustomer,我不能对你的函数做同样的事情吗?

                  Ok Makes sense, So now why even bother with having a GetCustomer Function? Can't I do that same thing with your function if i Just rename it GetCustomer?

                  好吧,这里发生的是调用者可以指定一些操作.假设 GetCustomers 是这样实现的:

                  Well, what's happening here is the caller can specify some action. Suppose GetCustomers is implemented like this:

                  public void GetCustomers(Action<Enumerable<Customer>, Exception> handleError) {
                      Customer[] customerlist =  GetCustomersFromDatabase();
                      try {
                          foreach(var c in customerList) 
                              c.ProcessSomething()
                      } catch (Exception e) {
                          handleError(customerList, e);
                      }
                  }
                  

                  然后你可以从命令行程序的某个地方调用 Getcustomers,然后传递它

                  then you could call Getcustomers from somewhere on a commandline program, and pass it

                  GetCustomers((list, exception) => { 
                      Console.WriteLine("Encountered error processing the following customers");
                      foreach(var customer in list) Console.WriteLine(customer.Name);
                      Console.WriteLine(exception.Message);
                  }); 
                  

                  例如,您可以从远程应用程序调用 GetCustomers 并传递它

                  while you could call GetCustomers from a remote application, for example, and pass it

                  Getcustomers((list, exception) => { 
                      // code that emails me the exception message and customer list
                  })
                  

                  <小时>此外,Slak 的评论提出了委托参数的另一个原因——GetCustomers 确实检索了客户,但是是异步的.每当它完成检索客户时,如果发生异常,它会使用客户列表或异常调用您提供给它的函数.


                  Also, Slak's comment suggests another reason for delegate parameter -- GetCustomers does retrieve the customers, but asynchronously. Whenever it is done retrieving the customers, it calls the function you give it with either the customerlist or an exception, if an exception occurred.

                  这篇关于请解释 .NET 代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)

                • <small id='idytC'></small><noframes id='idytC'>

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

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