是否可以部分扣除通用参数 [英] Is partial deduction of generic parameters possible

查看:55
本文介绍了是否可以部分扣除通用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我只是想检查一下我是否遗漏了什么东西,或者我是不是想要找b
。这是不可能的。 />

我有一个带有2个通用参数类型的函数,其中一个在参数列表中出现

,另一个没有。

所以函数是这样的:

void CreateEntities< T,IT>(IList< ITentities){}

我希望能够如下所示:

IList< IMyTypeentities = new List< IMyType>();

CreateEntities< MyType>(实体);


所以''IT''将被推断为''IMyType''和''T''被明确设置为

''MyType''。

这背后的原因是在函数中我将执行

跟随

entities.Add(new T());

我无法使用这种语法。


我必须:

CreateEntities< MyType,IMyType>(zh-CN tities);

如果我更改函数以获取类型为

''MyType''的额外无用参数,它将推断出参数。

所以这将起作用:

void CreateEntities< T,IT>(IList< ITentities,T t){}

CreateEntities(实体,新的MyType()) ;

但显然这不是一个有用的选项。

因此编译器可以推导出一种参数类型或者是

它全部还是全部?


谢谢,Vin

Hi,

I just wanted to check if I am missing something or if what I am
looking for isn''t possible.

I have a function with 2 generic parameter types, one of these occurs
in the argument list the other doesn''t.

So the function is like this:
void CreateEntities<T, IT>(IList<ITentities){}
I''d like to be able to call it as follows:
IList<IMyTypeentities = new List<IMyType>();
CreateEntities<MyType>(entities);

So ''IT'' would be deduced as ''IMyType'' and ''T'' is explicitly set to
''MyType''.

The reasoning behind this is that in the function I will do the
following
entities.Add(new T());
I haven''t been able to get this syntax to work.

I have to have:
CreateEntities<MyType, IMyType>(entities);
If I change the function to take an extra useless parameter of type
''MyType'' it''ll deduce the arguments.
So this will work:
void CreateEntities<T, IT>(IList<ITentities, T t){}
CreateEntities(entities, new MyType());
but clearly that isn''t a useful option.
So is it possible for the compiler to deduce one parameter type or is
it all or none?

Thanks, Vin

推荐答案

5月14日下午3:40,Vin < vfinn ... @ gmail.comwrote:


< snip>
On May 14, 3:40 pm, Vin <vfinn...@gmail.comwrote:

<snip>

因此编译器可能推导出一个参数类型或者是

全部还是全部?
So is it possible for the compiler to deduce one parameter type or is
it all or none?



全部或全部,我很害怕。


Jon

All or none, I''m afraid.

Jon

5月14日,3:44 * pm,Jon Skeet [C#MVP] < sk ... @ pobox.comwrote:
On May 14, 3:44*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:

全部或全部,我很害怕。
All or none, I''m afraid.



以为它可能是:o(


谢谢,Vin

Thought it might be :o(

Thanks, Vin


真的很遗憾......它会很好(特别是在使用

匿名类型时)能够混合''''匹配 - 也许类似于:


SomeFunc<?,int>(...)

(其中?表示推断)


我现在无法想到这些例子,但我知道我必须重构几次这样的事情来解决这个问题...主要是在没有任何问题的情况下

与特定参数相关的额外参数,或者

编译器本身无法完全完成工作的情况。当然,如果你有一个

匿名类型它变得非常难以帮助它!


(我们都有我们的C#愿望清单,这是我的......)


Marc
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix''n''match - perhaps something like:

SomeFunc<?,int>(...)

(where ? means "infer")

I can''t think of the examples right now, but I know I''ve had to refactor
things a few times to get around this... mainly where there isn''t any
additional argument relating to a specific argument, or where the
compiler can''t fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!

(we all have our C# wishlists, this is on mine...)

Marc


这篇关于是否可以部分扣除通用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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