<tfoot id='uQEPT'></tfoot>

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

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

      1. 类型参数来自 Action&lt;T&gt;不能推断,而是从 Func&lt;T&gt;

        type argument from Actionlt;Tgt; cannot be inferred, but from Funclt;Tgt; can be(类型参数来自 Actionlt;Tgt;不能推断,而是从 Funclt;Tgt;可)
        <tfoot id='1BZRn'></tfoot><legend id='1BZRn'><style id='1BZRn'><dir id='1BZRn'><q id='1BZRn'></q></dir></style></legend>
          <tbody id='1BZRn'></tbody>

        <small id='1BZRn'></small><noframes id='1BZRn'>

              • <bdo id='1BZRn'></bdo><ul id='1BZRn'></ul>
                <i id='1BZRn'><tr id='1BZRn'><dt id='1BZRn'><q id='1BZRn'><span id='1BZRn'><b id='1BZRn'><form id='1BZRn'><ins id='1BZRn'></ins><ul id='1BZRn'></ul><sub id='1BZRn'></sub></form><legend id='1BZRn'></legend><bdo id='1BZRn'><pre id='1BZRn'><center id='1BZRn'></center></pre></bdo></b><th id='1BZRn'></th></span></q></dt></tr></i><div id='1BZRn'><tfoot id='1BZRn'></tfoot><dl id='1BZRn'><fieldset id='1BZRn'></fieldset></dl></div>
                  本文介绍了类型参数来自 Action&lt;T&gt;不能推断,而是从 Func&lt;T&gt;可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在玩一些泛型和委托,但我发现了一些我不理解的东西.我有非常相似的通用静态方法,一个接受 Action<T>,第二个接受 Func<T>.现在的问题是:如果我调用没有显式类型的接受 Func<T> 的那个,编译器就可以了.但是接受 Action<T> 我的程序无法编译(请参阅错误消息的代码).

                  I've been playing a little with generics and delegates and I have found something I don't understand. I have quite similar generic static methods, one accepts Action<T> and the second one accepts Func<T>. Now the problem: if I call the one accepting Func<T> without explicit Type, compiler is fine with that. But with the one accepting Action<T> my program can't be compiled (see the code for error message).

                  我的问题是:为什么编译器能够识别返回类型,但不能识别参数类型?

                  My question is: Why is compiler able to recognize return type, but is not able to recognize argument type?

                  public interface IMessage
                  { }
                  
                  public class Message : IMessage
                  {
                  }
                  

                  static void HandleAction<TMessage>(Action<TMessage> action)
                      where TMessage : IMessage
                  { }
                  
                  static void HandleFunction<TMessage>(Func<TMessage> action)
                      where TMessage : IMessage
                  { }
                  
                  static void A(Message message)
                  { }
                  
                  static Message F()
                  {
                      return new Message();
                  }
                  
                  static void Main(string[] args)
                  {
                      // this one is ok
                      HandleFunction(F);
                  
                      // compiler error:
                      // The type arguments for method
                      // 'template_test.Program.HandleAction<TMessage>(System.Action<TMessage>)' 
                      // cannot be inferred from the usage.
                      //Try specifying the type arguments explicitly.
                      //HandleAction(A);
                  
                      // this one is ok
                      HandleAction<Message>(A);
                  }
                  

                  我在 Visual Studio 2012 中使用 .NET 4.5.

                  I'm using .NET 4.5 in Visual Studio 2012.

                  推荐答案

                  方法可以通过其参数重载,所有重载形成一个方法组,例如 void Xyz(int i)void Xyz(string s) 位于名为 Xyz 的同一方法组中.即使用户只定义了一个方法,编译器也无法推断出一种类型的参数,因为编译器的行为非常严格.

                  Methods can be overloaded by their arguments and all overloads form one method group, so for example void Xyz(int i) and void Xyz(string s) are within same method group called Xyz. Compiler is not able to deduct a type of argument even if user defines only one method, because behaviour of compiler is quite strict.

                  方法不能被返回类型重载,所以你不能在同一个类中有 int Xyz()string Xyz().返回类型很容易被编译器推断出来,因为没有重载.

                  Methods can't be overloaded by return types, so you can't have int Xyz() and string Xyz() within same class. Return type can be deducted by compiler easily, because there is no overloading.

                  第一次对我来说不是很明显,但是当我意识到我可以创建一个重载之后就很清楚了.

                  It was not obvious for me for the first time, but it has been quite clear after I realized that I could create an overload.

                  这篇关于类型参数来自 Action&lt;T&gt;不能推断,而是从 Func&lt;T&gt;可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

                  <tfoot id='4tvmf'></tfoot>

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

                            <small id='4tvmf'></small><noframes id='4tvmf'>