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

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

问题描述

我试图创建一个通用的扩展方法,即对工作类型的数据表:

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 的类型。结果
这是将被用作许多不同的程序员数据层的一部分的方法,所以我宁愿不需要他们指定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 ?

对于不同的原因,可能无法在本code样本中是显而易见的,我真的需要在其原来的形式返回的数据表。我需要的原因 ROWTYPE 的是为了让'防爆pression &LT; Func键&LT; T,布尔> 方式&gt; '将被输入和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:

<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx\">http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

我坚持我的立场。

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

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