<tfoot id='aYQ7A'></tfoot>
    <legend id='aYQ7A'><style id='aYQ7A'><dir id='aYQ7A'><q id='aYQ7A'></q></dir></style></legend>

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

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

      1. .NET 4.0 中的协变和逆变错误

        Co- and Contravariance bugs in .NET 4.0(.NET 4.0 中的协变和逆变错误)

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

          • <tfoot id='CgQQ4'></tfoot>
              <bdo id='CgQQ4'></bdo><ul id='CgQQ4'></ul>
                <tbody id='CgQQ4'></tbody>

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

                1. 本文介绍了.NET 4.0 中的协变和逆变错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  C# 4.0 协变和逆变支持的一些奇怪行为:

                  Some strange behavior with the C# 4.0 co- and contravariance support:

                  using System;
                  
                  class Program {
                    static void Foo(object x) { }
                    static void Main() {
                      Action<string> action = _ => { };
                  
                      // C# 3.5 supports static co- and contravariant method groups
                      // conversions to delegates types, so this is perfectly legal:
                      action += Foo;
                  
                      // since C# 4.0 much better supports co- and contravariance
                      // for interfaces and delegates, this is should be legal too:
                      action += new Action<object>(Foo);
                    }
                  }
                  

                  ArgumentException: Delegates must be the same type 的结果.

                  很奇怪,不是吗?为什么 Delegate.Combine()(在对委托执行 += 操作时调用)在运行时不支持协变和逆变?

                  Strange, isn't it? Why Delegate.Combine() (which is been called when performing += operation on the delegates) does not support co- and contravariance at runtime?

                  此外,我发现 BCL 的 System.EventHandler 委托类型在其通用 TEventArgs 参数上没有逆变注释!为什么?这是完全合法的, TEventArgs 类型仅用于输入位置.也许没有逆变注释,因为它很好地隐藏了 Delegate.Combine() 的错误?;)

                  Moreover, I've found that BCL's System.EventHandler<TEventArgs> delegate type does not has contravariant annotation on it's generic TEventArgs parameter! Why? It's perfectly legal, TEventArgs type used only at input position. Maybe there is no contravariant annotation because of it nicely hides the bug with the Delegate.Combine()? ;)

                  附言所有这些都会影响 VS2010 RC 及更高版本.

                  p.s. All this affects the VS2010 RC and later versions.

                  推荐答案

                  长话短说:就方差而言,委托组合都搞砸了.我们在周期的后期发现了这一点.我们正在与 CLR 团队合作,看看我们是否可以想出一些方法来使所有常见场景都工作而不会破坏向后兼容性等等,但是我们想出的任何东西都可能不会进入 4.0 版本.希望我们能在一些服务包中解决所有问题.对于给您带来的不便,我深表歉意.

                  Long story short: delegate combining is all messed up with respect to variance. We discovered this late in the cycle. We're working with the CLR team to see if we can come up with some way to make all the common scenarios work without breaking backwards compatibility, and so on, but whatever we come up with will probably not make it into the 4.0 release. Hopefully we'll get it all sorted out in some service pack. I apologize for the inconvenience.

                  这篇关于.NET 4.0 中的协变和逆变错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='QW5MT'></bdo><ul id='QW5MT'></ul>
                        • <small id='QW5MT'></small><noframes id='QW5MT'>

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

                            <tfoot id='QW5MT'></tfoot>

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