我试图创建一个通用的扩展方法,即在输入数据表的工作原理:通用扩展方法:类型参数不能从使用推断
public static class Extensions
{
public static TableType DoSomething<TableType, RowType>(this TableType table, param Expression<Func<RowType, bool>>[] predicates)
where TableType : TypedTableBase<RowType>
where RowType : DataRow
{
// do something to each row of the table where the row matches the predicates
return table;
}
[STAThread]
public static void main()
{
MyTypedDataSet.MyTypedDataTable table = getDefaultTable();
}
public static MyTypedDataSet.MyTypedDataTable getDefaultTable()
{
// this line compiles fine and does what I want:
return new MyTypedDataSet.MyTypedDataTable().DoSomething<MyTypedDataSet.MyTypedDataTable, MyTypedDataSet.MyTypedRow>(row => row.Field1 == "foo");
// this line doesn't compile :
return new MyTypedDataSet.MyTypedDataTable().DoSomething(row => row.Field1 == "foo");
// Error : The type arguments .. cannot be inferred from the usage
}
}
第一线工作正常,但它真的很丑陋...
第二行不编译,因为编译器无法推断出RowType类型的类型。
这是一种将被许多不同程序员用作DataLayer的一部分的方法,所以我宁愿不需要它们来指定TypeParameter。
编译器不应该知道RowType与TypedTableBase使用的类型相同吗?
由于在此代码示例中可能不明显的不同原因,我确实需要以其原始形式返回数据表。而我需要的原因RowType是这样的'Expression <
Func <
T,bool>>
'将被键入并且被InteliSence看到。
感谢
我很害怕那个......谢谢你的回答 – 2010-09-02 19:14:59
你是对的:) – Brian 2010-09-02 21:43:38