<legend id='OoDJO'><style id='OoDJO'><dir id='OoDJO'><q id='OoDJO'></q></dir></style></legend>
      <bdo id='OoDJO'></bdo><ul id='OoDJO'></ul>

    1. <tfoot id='OoDJO'></tfoot>

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

      <i id='OoDJO'><tr id='OoDJO'><dt id='OoDJO'><q id='OoDJO'><span id='OoDJO'><b id='OoDJO'><form id='OoDJO'><ins id='OoDJO'></ins><ul id='OoDJO'></ul><sub id='OoDJO'></sub></form><legend id='OoDJO'></legend><bdo id='OoDJO'><pre id='OoDJO'><center id='OoDJO'></center></pre></bdo></b><th id='OoDJO'></th></span></q></dt></tr></i><div id='OoDJO'><tfoot id='OoDJO'></tfoot><dl id='OoDJO'><fieldset id='OoDJO'></fieldset></dl></div>
    2. Delegate.CreateDelegate() 和泛型:错误绑定到目标方法

      时间:2023-11-11
        <bdo id='Vgfth'></bdo><ul id='Vgfth'></ul>

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

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

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

                <tbody id='Vgfth'></tbody>
              1. 本文介绍了Delegate.CreateDelegate() 和泛型:错误绑定到目标方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在使用反射和泛型创建委托集合时遇到问题.

                I'm having problems creating a collection of delegate using reflection and generics.

                我正在尝试从 Ally 方法创建一个委托集合,这些方法共享一个公共方法签名.

                I'm trying to create a delegate collection from Ally methods, whose share a common method signature.

                public class Classy
                {
                  public string FirstMethod<T1, T2>( string id, Func<T1, int, IEnumerable<T2>> del );
                  public string SecondMethod<T1, T2>( string id, Func<T1, int, IEnumerable<T2>> del );    
                  public string ThirdMethod<T1, T2>( string id, Func<T1, int, IEnumerable<T2>> del );
                
                  // And so on...
                }
                

                还有泛型烹饪:

                // This is the Classy's shared method signature    
                public delegate string classyDelegate<out T1, in T2>( string id, Func<T1, int, IEnumerable<T2>> filter );
                
                
                // And the linq-way to get the collection of delegates from Classy
                ( 
                   from method in typeof( Classy ).GetMethods( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic )
                   let delegateType = typeof( classyDelegate<,> )
                   select Delegate.CreateDelegate( delegateType, method )
                ).ToList( );
                

                但是 Delegate.CreateDelegate(delegateType, method) 抛出一个 ArgumentException 说 Error binding to target method.:/

                But the Delegate.CreateDelegate( delegateType, method ) throws an ArgumentException saying Error binding to target method. : /

                我做错了什么?

                推荐答案

                那是因为Delegate.CreateDelegate的重载只支持创建指向静态方法的委托.如果要绑定到实例方法,还需要传入您创建的委托应该调用该方法的实例.

                That is because the overload of Delegate.CreateDelegate only supports creating delegates pointing to static methods. If you want to bind to instance methods, you also need to pass in the instance on which your created delegate is supposed to call the method.

                你可能想要:

                from method in typeof( Classy ).GetMethods( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic )
                let delegateType = typeof( classyDelegate<,> )
                select Delegate.CreateDelegate( delegateType, yourInstance, method )
                

                另外,您的代码示例无法编译.您不能在方法签名上声明方差;并且不能省略非抽象类中的实现.

                Also, your code example won't compile. You can't declare variance on method signatures; and you can't omit the implementation in a non-abstract class.

                最后,Delegate.CreateDelegate 创建了一个Delegateinstance,如果不知道它的类型参数就无法存在.因此,您不能绑定到 classyDelegate<,>,您需要知道所涉及的实际类型.

                Finally, Delegate.CreateDelegate creates a Delegate instance, which cannot exist without knowing it's type parameters. Therefore, you cannot bind to classyDelegate<,>, you need to know the actual types involved.

                这篇关于Delegate.CreateDelegate() 和泛型:错误绑定到目标方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:函数委托与函数 下一篇:没有 EndInvoke 的 C# 异步调用?

                相关文章

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

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

                  1. <legend id='IyGF4'><style id='IyGF4'><dir id='IyGF4'><q id='IyGF4'></q></dir></style></legend>