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?