C#:为什么没有泛型类型推断的工作时,有多个类型的参数? [英] C#: Why doesn't generic type inference work when there are multiple type arguments?

查看:172
本文介绍了C#:为什么没有泛型类型推断的工作时,有多个类型的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是两个样本:



这工作得很好:

 无效的主要()
{
无功名单= Queryable.ProjectTo(typeof运算(投影));
}

公共静态类QueryableExtensions
{
公共静态ProjectionList< T> ProjectTo< T>(这IQueryable的< T>可查询类型projectionType)
{
返回新ProjectionList< T>(可查询,projectionType);
}
}

这引发以下错误:




使用泛型方法
'QueryableExtensions.ProjectTo(System.Linq.IQueryable)'
需要2个类型参数




 无效的主要()
{
无功名单= Queryable.ProjectTo<投影>( );
}

公共静态类QueryableExtensions
{
公共静态ProjectionList< T,P> ProjectTo< T,P>(这IQueryable的< T>可查询)
{
返回新ProjectionList< T,P>(可查询);
}
}



当然,第一个例子需要1个类型参数,但是编译器可以计算出它是什么,所以我并不需要提供任何。第二个例子需要2个类型参数,但是编译器知道T是什么,为什么它不仅要求不能被推断出来的?



有关记录我使用第一个例子只是在我的代码很好,但我喜欢第二个更好的语法并有可能在这里我想有通用型投影的情况。有什么办法来实现这个还是我找错了树?



谢谢!


解决方案

这个问题是不是2个参数,而是:从那里将它推断呢?泛型参数推断只看参数,特别是不考虑返回类型。没有任何的参数会建议P.这是必需的,无论是通用的类型推断为他们提供的全部或所有的人都明确指定。有趣的是,曾经提到它,因为我理解它(因为它从来没有得到完全确定)将允许你随意搭配,像你想咕哝打字的。想象一下:



  blah.ProjectTo<,&SOMETYPE GT;(); 



(确切的语法并不重要,因为这种语言特性不存在) - 但它会意思是有2 genericmtype参数;你(编译器)计算出的第一个参数;二是SOMETYPE。


Here are two samples:

This works fine:

void Main()
{
    var list = Queryable.ProjectTo(typeof(Projection));
}

public static class QueryableExtensions
{
    public static ProjectionList<T> ProjectTo<T>(this IQueryable<T> queryable, Type projectionType)
    {
        return new ProjectionList<T>(queryable, projectionType);
    }
}

This throws the following error:

Using the generic method 'QueryableExtensions.ProjectTo(System.Linq.IQueryable)' requires 2 type arguments

void Main()
{
    var list = Queryable.ProjectTo<Projection>();
}

public static class QueryableExtensions
{
    public static ProjectionList<T, P> ProjectTo<T, P>(this IQueryable<T> queryable)
    {
        return new ProjectionList<T, P>(queryable);
    }
}

Of course the first example requires 1 type argument, however the compiler can figure out what it is so I don't need to supply any. The second example requires 2 type arguments but the compiler knows what T is so why does it not only require the one that cannot be inferred?

For the record I am using the first example just fine in my code, but I like the syntax of the second much better and there may be a case where I would like to have the generic type of the projection. Is there any way to achieve this or am I barking up the wrong tree?

Thanks!

解决方案

The issue is not the 2 parameters, but rather: from where would it infer it? Generic parameter inference only looks at parameters, and specifically does not consider return types. There is nothing in the parameters that would suggest P. It is required that either generic type inference provides all of them, or all of them are specified explicitly. Interestingly, there was once mention of "mumble-typing" which, as I interpret it (since it never got defined fully) would have allowed you to mix and match, like you want. Imagine:

blah.ProjectTo<?,SomeType>();

(the exact syntax doesn't matter since this language feature doesn't exist) - but it would mean "there are 2 genericmtype arguments; you (the compiler) figure out the first parameter; the second is SomeType".

这篇关于C#:为什么没有泛型类型推断的工作时,有多个类型的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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