<bdo id='INwXs'></bdo><ul id='INwXs'></ul>
    1. <small id='INwXs'></small><noframes id='INwXs'>

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

    3. <legend id='INwXs'><style id='INwXs'><dir id='INwXs'><q id='INwXs'></q></dir></style></legend>
      <tfoot id='INwXs'></tfoot>
    4. 通用接口方法的开放委托

      时间:2023-11-10
      <tfoot id='Fpe6a'></tfoot>
        <tbody id='Fpe6a'></tbody>
              <bdo id='Fpe6a'></bdo><ul id='Fpe6a'></ul>

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

              1. <i id='Fpe6a'><tr id='Fpe6a'><dt id='Fpe6a'><q id='Fpe6a'><span id='Fpe6a'><b id='Fpe6a'><form id='Fpe6a'><ins id='Fpe6a'></ins><ul id='Fpe6a'></ul><sub id='Fpe6a'></sub></form><legend id='Fpe6a'></legend><bdo id='Fpe6a'><pre id='Fpe6a'><center id='Fpe6a'></center></pre></bdo></b><th id='Fpe6a'></th></span></q></dt></tr></i><div id='Fpe6a'><tfoot id='Fpe6a'></tfoot><dl id='Fpe6a'><fieldset id='Fpe6a'></fieldset></dl></div>
                <legend id='Fpe6a'><style id='Fpe6a'><dir id='Fpe6a'><q id='Fpe6a'></q></dir></style></legend>
                本文介绍了通用接口方法的开放委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试为通用接口方法创建一个 开放实例委托,但我不断收到 NotSupportedException.这是不会运行的简化代码:

                I'm trying to create an open instance delegate for a generic interface method, but I keep receiving a NotSupportedException. Here is the simplified code that won't run:

                interface IFoo
                {
                    void Bar<T>(T j);
                }
                class Foo : IFoo
                {
                    public void Bar<T>(T j)
                    {
                    }
                }
                static void Main(string[] args)
                {
                    var bar = typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int));
                    var x = Delegate.CreateDelegate(typeof(Action<IFoo, int>), null, bar);
                }
                

                最后一行抛出 NotSupportedException,不支持指定的方法".相比之下,非泛型开放实例委托运行良好:

                The last line throws NotSupportedException, "Specified method is not supported". By comparison, a non-generic open instance delegate runs fine:

                interface IFoo
                {
                    void Bar(int j);
                }
                class Foo : IFoo
                {
                    public void Bar(int j)
                    {
                    }
                }
                static void Main(string[] args)
                {
                    var bar = typeof(IFoo).GetMethod("Bar");
                    var x = Delegate.CreateDelegate(typeof(Action<IFoo, int>), null, bar);
                }
                

                封闭的通用委托也可以:

                And a closed generic delegate also works:

                interface IFoo
                {
                    void Bar<T>(T j);
                }
                class Foo : IFoo
                {
                    public void Bar<T>(T j)
                    {
                    }
                }
                static void Main(string[] args)
                {
                    var bar = typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int));
                    var x = Delegate.CreateDelegate(typeof(Action<int>), new Foo(), bar);
                }
                

                因此,封闭式泛型委托和开放式实例委托的秘诀是分开工作,但在组合时则不然.它开始看起来像是运行时错误或故意遗漏.有人对此有任何见解吗?

                So the recipe for closed generic delegates and open instance delegates work separately, but not when combined. It's starting to look like either a runtime bug, or intentional omission. Anyone have any insight here?

                推荐答案

                微软已经回答,CLR 无法做到这一点是一个已知问题,但在当前版本的 .网.正如我在那儿解释的那样,为什么这仍然是不可能的.出于某种原因,开放委托不能重用 CLR 中其他地方使用的调度逻辑,这对我来说似乎很奇怪.

                Microsoft has answered that it's a known problem that the CLR can't do this, but it can't be solved in the current version of .NET. It's still not at all clear why this isn't possible as I explain there. Open delegates must not reuse the dispatching logic used everywhere else in the CLR for some reason, which just seems bizarre to me.

                这篇关于通用接口方法的开放委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么是 Func&lt;&gt;从 Expression&lt;Func&lt;&am 下一篇:在值类型上定义的扩展方法不能用于创建委托 - 为什么不呢?

                相关文章

                  • <bdo id='N2x20'></bdo><ul id='N2x20'></ul>

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

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