使用带有泛型的语句:使用 ISet<> = System.Collections.Generic.ISet>>;
由于我使用两个不同的通用集合命名空间(System.Collections.Generic
和 Iesi.Collections.Generic
),因此存在冲突。在项目的其他部分,我同时使用 nunit 和 mstest 框架,但限定当我调用 Assert
时,我想使用 nunit 版本,
using Assert = NUnit.Framework.Assert;
效果很好,但我想做同样的事情具有泛型类型的东西。但是,以下行不起作用
using ISet = System.Collections.Generic.ISet;
using ISet<> = System.Collections.Generic.ISet<>;
有谁知道如何告诉 .net 如何将 using 语句与泛型一起使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不幸的是,
using
指令并不能满足您的要求。你可以说:and
但你不能说
or ,
这是语言从未被纠正的缺点。
Unfortunately, the
using
directive does not do what you want. You can say:and
but you cannot say
or
It's a shortcoming of the language that has never been rectified.
我认为你最好给命名空间本身起别名,而不是给泛型类型起别名(我认为这是不可能的)。
例如:
那么对于 BCL
ISet
,例如:I think you're better off aliasing the namespaces themselves as opposed to the generic types (which I don't think is possible).
So for instance:
Then for a BCL
ISet<int>
, for example:为泛型类型添加别名的唯一方法是将其专门化,如下所示。
您不能像在示例中所做的那样为开放泛型类型添加别名:
The only way you can alias a generic type is to specialize it as follows.
You can not alias an open generic type as you have done in your example:
您的别名与类名本身相同,因此仍然存在歧义,就像每个命名空间都有一个
using
一样。为类的别名指定一个不同的名称,即:Your alias name is the same as the class name itself, so you still have ambiguity, just as if you had a
using
for each namespace. Give the alias of the class a different name, i.e.:在某些情况下,您可以使用继承:
In some cases you can go with inheritance:
您可以为类添加别名:
仅当该类不是通用类时。
You can alias a class doing :
Only if the class is NOT generic.