This question already has answers here:
Using Statement with Generics: using ISet<> = System.Collections.Generic.ISet<>

(6个答案)


7年前关闭。




因此有时我只想包含一个 namespace 中的一个类,而不是整个 namespace ,例如这里的示例,我使用using语句为该类创建别名:
using System;
using System.Text;
using Array = System.Collections.ArrayList;

我经常使用泛型来执行此操作,因此不必重复这些参数:
using LookupDictionary = System.Collections.Generic.Dictionary<string, int>;

现在,我想使用泛型类型完成相同的操作,同时将其保留为泛型类型:
using List<T> = System.Collections.Generic.List<T>;

但这不能编译,因此有什么方法可以在不将类型保留为泛型的情况下创建此别名?

最佳答案

不,那里没有。 C#中的类型别名必须为封闭(也称为完全解析)类型,因此不支持开放泛型

C#语言规范的9.4.1节对此进行了介绍。


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
}

关于c# - 使用带有通用类型的 'using alias = class'吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4936941/

10-10 16:43
查看更多