Delegate.CreateDelegate() and generics: Error binding to target method

Posted by SDReyes on Stack Overflow See other posts from Stack Overflow or by SDReyes
Published on 2010-04-26T16:13:42Z Indexed on 2010/04/26 16:23 UTC
Read the original article Hit count: 817

Filed under:
|
|
|
|

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

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

public class Classy
{
  public string FirstMethod<out T1, in T2>( string id, Func<T1, int, IEnumerable<T2>> del );
  public string SecondMethod<out T1, in T2>( string id, Func<T1, int, IEnumerable<T2>> del );    
  public string ThirdMethod<out T1, in T2>( string id, Func<T1, int, IEnumerable<T2>> del );

  // And so on...
}

And the generics cooking:

// 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( );

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

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about generics