通用扩展方法:无法从用法推断出类型参数 [英] Generic extension method : Type argument cannot be inferred from the usage

查看:21
本文介绍了通用扩展方法:无法从用法推断出类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个适用于类型化数据表的通用扩展方法:

I'm trying to create a generic extension method, that works on typed data tables :

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 使用的类型相同吗?

The first line works fine, but it's really ugly...
The second line doesn't compile because the compiler cannot infer the type of RowType.
This is a method that will be used as part of a DataLayer by many different programmers, so I would rather not need them to specify the TypeParameter.
Shouldn't the compiler know that RowType is the same type as the one that was used by TypedTableBase ?

出于在此代码示例中可能并不明显的各种原因,我确实需要以原始形式返回数据表.我需要 RowType 的原因是 'Expression<Func<T, bool>>' 将由 InteliSence 输入并查看.

For different reasons that may not be obvious in this code sample, I really need to return the datatable in its original form. And the reason I need RowType is so the 'Expression<Func<T, bool>>' will be typed and seen by InteliSence.

谢谢

推荐答案

方法类型推断不会从参数到约束进行推断.它从实参到形参进行推断,然后检查从实参到形参的推断是否满足约束.

Method type inference does not make inferences from arguments to constraints. It makes inferences from arguments to formal parameters and then checks whether the inferences made from the arguments to the formals satisfy the constraints.

在您的情况下,没有足够的参数数据来推断类型参数是什么,而无需先查看约束,我们不会这样做直到我们根据约束检查推断.抱歉,这就是指定类型推断算法的方式.

In your case there is not enough data from the arguments to deduce what the type parameters are without first looking at the constraints, which we're not going to do until we check the inferences against the constraints. Sorry about that, but that's how the type inference algorithm is specified.

我已多次被问到有关此问题的问题,并且一致认为我坚持推理应该仅从参数到形式参数进行推断的立场在道德上是错误的.对于大约十几个人告诉我我在这方面的错误,请参阅我对这个密切相关问题的分析的评论:

I've been asked questions about this many times and the consensus seems to be that I am morally wrong for maintaining the position that inference should infer from arguments to formal parameters alone. For about a dozen people telling me I'm wrongheaded in this regard, see the comments to my analysis of this closely related issue:

http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

我保持我的位置.

这篇关于通用扩展方法:无法从用法推断出类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆