在泛型中使用Statement:使用ISet <> = System.Collections.Generic.ISet <>

Jas*_*ore 40 c# generics syntax alias using

由于我使用两个不同的通用集合名称空间(System.Collections.GenericIesi.Collections.Generic),我有冲突.在项目的其他部分,我使用的是nunit和mstest框架,但是当我打电话时Assert我想要使​​用nunit版本

using Assert = NUnit.Framework.Assert;
Run Code Online (Sandbox Code Playgroud)

哪个效果很好,但我想用泛型类型做同样的事情.但是,以下行不起作用

using ISet = System.Collections.Generic.ISet;
using ISet<> = System.Collections.Generic.ISet<>;
Run Code Online (Sandbox Code Playgroud)

有谁知道如何告诉.net如何使用泛型的using语句?

Eri*_*ert 38

不幸的是,该using指令没有做你想要的.你可以说:

using Frob = System.String;
Run Code Online (Sandbox Code Playgroud)

using ListOfInts = System.Collections.Generic.List<System.Int32>;
Run Code Online (Sandbox Code Playgroud)

但你不能说

using Blob<T> = System.Collections.Generic.List<T>
Run Code Online (Sandbox Code Playgroud)

要么

 using Blob = System.Collections.Generic.List
Run Code Online (Sandbox Code Playgroud)

这是从未纠正过的语言的缺点.

  • [此处]有一个提案(https://github.com/dotnet/csharplang/issues/1239)。我也很想拥有这个功能。 (3认同)
  • 游说此功能的最佳方式是什么? (2认同)

Dan*_*Tao 37

我认为你最好别名命名空间本身,而不是泛型类型(我认为不可能).

例如:

using S = System.Collections.Generic;
using I = Iesi.Collections.Generic;
Run Code Online (Sandbox Code Playgroud)

然后对于BCL ISet<int>,例如:

S.ISet<int> integers = new S.HashSet<int>();
Run Code Online (Sandbox Code Playgroud)


Ste*_*idi 6

您可以为泛型类别设置别名的唯一方法是将其专门化,如下所示.

using IntSet = System.Collections.Generic.ISet<int>;
Run Code Online (Sandbox Code Playgroud)

您不能像在示例中那样使用开放泛型类型的别名:

using MySet = System.Collections.Generic.ISet<>;
Run Code Online (Sandbox Code Playgroud)