.Net 支持柯里化泛型吗?
假设我们有一个嵌套泛型类:
public class A<T> {
public class B<U> { }
}
这里,typeof(A
本质上是一个具有两个参数的泛型类,其中仅绑定第一个参数。
如果我有一个带有两个参数的类,
public class AB<T, U> { }
有没有办法引用“AB
,其中 T=int
和 U
保持打开状态”?如果不是,这是 C# 限制还是 CLR 限制?
Suppose we have a nested generic class:
public class A<T> {
public class B<U> { }
}
Here, typeof(A<int>.B<>)
is in essence a generic class with two parameters where only the first is bound.
If I have a single class with two parameters
public class AB<T, U> { }
Is there a way to refer to "AB
with T=int
and U
staying open"? If not, is this a C# limitation, or a CLR limitation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然这不能在 C# 中完成,您必须指定两个类型参数,或者都不指定。
而且 CLR 似乎也不支持,
A.B<>
和A.B<>
指的是相同类型:两种类型的封闭类型是
A<>
(开放泛型类型)编辑:进一步测试表明
typeof(A.B)
实际上是 arity 2 的泛型类型,不是 arity 1 的嵌套泛型类型...typeof(A.B).GetGenericArguments()
返回一个带有typeof(int)
和typeof(string)
的数组。因此typeof(A.B<>)
实际上相当于(AB)
,这是不支持的(泛型类型不能部分关闭)Apparently it can't be done in C#, you have to specify either both type parameters, or none.
And it doesn't seem to be supported by the CLR either,
A<int>.B<>
andA<string>.B<>
refer to the same type:The enclosing type of both types is
A<>
(open generic type)EDIT: further testing shows that
typeof(A<int>.B<string>)
is actually a generic type of arity 2, not a nested generic type of arity 1...typeof(A<int>.B<string>).GetGenericArguments()
returns an array withtypeof(int)
andtypeof(string)
. Sotypeof(A<int>.B<>)
would actually be equivalent to(A.B)<int, >
, which isn't supported (a generic type can't be partially closed)这是你的想法吗?
Is this what you have in mind?
您可以使用 Func;泛型将多个参数作为返回类型的单个函数传递。柯里化了很多,将许多参数作为单个参数传递。
you can use the Func<int,int> generic to pass multiple parameters as a single function returning an type. Curried many to pass many arguments as a single parameter.