【发布时间】:2010-04-26 16:13:42
【问题描述】:
我在使用反射和泛型创建委托集合时遇到问题。
我正在尝试从 Ally 方法创建一个委托集合,这些方法共享一个公共方法签名。
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 说错误绑定到目标方法。 :/
我做错了什么?
【问题讨论】:
标签: c# .net generics exception delegates