泛型方法中的Where子句

时间:2014-04-09 18:49:46

标签: c# generics

我有一个通用方法,需要2个泛型参数:BType和IType。有没有办法在方法签名中定义BType必须是IType的实现?!?

Edit1:这里是方法签名:

public static Expression<Func<BType, bool>> CastFunc<BType,IType>(Expression<Func<IType, bool>> customWhereClause)

2 个答案:

答案 0 :(得分:10)

public void Foo<BType, IType>(BType a, IType b) where BType : IType

答案 1 :(得分:3)

public static Expression<Func<BType, bool>> CastFunc<BType,IType>(Expression<Func<IType, bool>> customWhereClause) where BType : IType

文档&#34;类型参数的约束&#34;:http://msdn.microsoft.com/en-us/library/d5x73970.aspx