使用'using别名=阶级“与一般类型? [英] Using a 'using alias = class' with generic types?
问题描述
所以有时候我想喜欢这里的例子中,我创建一个别名的类using语句包含来自一个命名空间而不是整个空间只有一个类,:
使用系统;
使用System.Text;使用数组
= System.Collections.ArrayList;
我经常这样做与泛型,这样我就不必赘述了>
使用LookupDictionary = System.Collections.Generic.Dictionary<字符串,整数取代;
现在我要完成一个泛型类型相同,同时保留它作为一个泛型类型:
使用列表< T> = System.Collections.Generic.List< T> ;;
但是,这并不编译,那么有没有什么办法来实现创建这个别名而留下的类型作为通用的?
没有没有。在C#类型别名必须是封闭(又名完全解决)型如此开放泛型是不支持
这是覆盖在C#语言规范第9.4.1。
使用别名可以命名一个封闭构造类型,但是的不能没有提供类型的参数名称绑定的泛型类型的声明。
块引用>
命名空间N2
{
使用W = N1.A; //错误,不能使用命名X = N1.A.B未绑定的泛型类型
; //错误,不能使用命名Y = N1.A<未绑定的泛型类型
; INT取代; // OK,可以关闭名使用z,其中构造类型
; T> = N1.A< T> ;; //错误,使用别名不能有类型参数
}
So sometimes I want to include only one class from a namespace rather than a whole namespace, like the example here I create a alias to that class with the using statement:
using System; using System.Text; using Array = System.Collections.ArrayList;
I often do this with generics so that I don't have to repeat the arguments:
using LookupDictionary = System.Collections.Generic.Dictionary<string, int>;
Now I want to accomplish the same with a generic type, while preserving it as a generic type:
using List<T> = System.Collections.Generic.List<T>;
But that doesn't compile, so is there any way to achieve creating this alias while leaving the type as generic?
解决方案No there is not. A type alias in C# must be a closed (aka fully resolved) type so open generics are not supported
This is covered in section 9.4.1 of the C# Language spec.
Using aliases can name a closed constructed type, but cannot name an unbound generic type declaration without supplying type arguments.
namespace N2 { using W = N1.A; // Error, cannot name unbound generic type using X = N1.A.B; // Error, cannot name unbound generic type using Y = N1.A<int>; // Ok, can name closed constructed type using Z<T> = N1.A<T>; // Error, using alias cannot have type parameters }
这篇关于使用'using别名=阶级“与一般类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!