【发布时间】:2013-07-25 21:31:45
【问题描述】:
有没有办法实现一个泛型类型的扩展方法,它接受另一个类型的 Func 参数?
例如,类似这样的用法:
myFirstObject.Extension<myOtherObject>( other => other.Prop );
或者使用更复杂的 Func:
myFirstObject.Extension<myOtherObject>( other => other.Prop > 2 && other.Prop < 15 );
我发现了一些相关问题,例如this one,但就我而言,我也需要扩展方法中的泛型类型。
这是我想出的:
public static bool Extension<TSource, TIn, TKey>(this TSource p_Value, Expression<Func<TIn, TKey>> p_OutExpression)
{ return true; }
但是,当我尝试使用它时,它并没有考虑到第二种类型。
我错过了什么吗?
【问题讨论】:
标签: linq generics types extension-methods func