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

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

问题描述

我试图创建一个通用的扩展方法,它适用于类型化的数据表:

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。

不应该编译器知道是与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 ?

由于不同的原因,这个代码可能并不明显sample,我真的需要返回它的原始形式的datatable。我需要 RowType 的原因是Expression < Func < T ,

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.

感谢

推荐答案

方法类型推理不会从参数推论到约束。它从参数推理到形式参数,然后检查从参数到formals的推断是否满足约束。

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天全站免登陆