使用带有泛型的语句:使用 ISet<> = System.Collections.Generic.ISet>>;

发布于 2024-09-18 23:19:41 字数 476 浏览 3 评论 0 原文

由于我使用两个不同的通用集合命名空间(System.Collections.GenericIesi.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 语句与泛型一起使用?

Since I am using two different generic collection namespaces (System.Collections.Generic and Iesi.Collections.Generic), I have conflicts. In other parts of the project, I am using both the nunit and mstest framework, but qualify that when I call Assert I want to use the nunit version by

using Assert = NUnit.Framework.Assert;

Which works great, but I want to do the same thing with generic types. However, the following lines do not work

using ISet = System.Collections.Generic.ISet;
using ISet<> = System.Collections.Generic.ISet<>;

Does anyone know how to tell .net how to use the using statement with generics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨落画卷 2024-09-25 23:19:41

不幸的是, using 指令并不能满足您的要求。你可以说:

using Frob = System.String;

and

using ListOfInts = System.Collections.Generic.List<System.Int32>;

但你不能说

using Blob<T> = System.Collections.Generic.List<T>

or ,

using Blob = System.Collections.Generic.List

这是语言从未被纠正的缺点。

Unfortunately, the using directive does not do what you want. You can say:

using Frob = System.String;

and

using ListOfInts = System.Collections.Generic.List<System.Int32>;

but you cannot say

using Blob<T> = System.Collections.Generic.List<T>

or

using Blob = System.Collections.Generic.List

It's a shortcoming of the language that has never been rectified.

不寐倦长更 2024-09-25 23:19:41

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

例如:

using S = System.Collections.Generic;
using I = Iesi.Collections.Generic;

那么对于 BCL ISet,例如:

S.ISet<int> integers = new S.HashSet<int>();

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:

using S = System.Collections.Generic;
using I = Iesi.Collections.Generic;

Then for a BCL ISet<int>, for example:

S.ISet<int> integers = new S.HashSet<int>();
半边脸i 2024-09-25 23:19:41

为泛型类型添加别名的唯一方法是将其专门化,如下所示。

using IntSet = System.Collections.Generic.ISet<int>;

您不能像在示例中所做的那样为开放泛型类型添加别名:

using MySet = System.Collections.Generic.ISet<>;

The only way you can alias a generic type is to specialize it as follows.

using IntSet = System.Collections.Generic.ISet<int>;

You can not alias an open generic type as you have done in your example:

using MySet = System.Collections.Generic.ISet<>;
陈年往事 2024-09-25 23:19:41

您的别名与类名本身相同,因此仍然存在歧义,就像每个命名空间都有一个 using 一样。为类的别名指定一个不同的名称,即:

using FirstNamespace;
using OtherObject = SecondNamespace.MyObject;

public class Foo
{
    public void Bar()
    {
        MyObject first = new MyObject;//will be the MyObject from the first namespace
        OtherObject second = new OtherObject;
    }
}

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.:

using FirstNamespace;
using OtherObject = SecondNamespace.MyObject;

public class Foo
{
    public void Bar()
    {
        MyObject first = new MyObject;//will be the MyObject from the first namespace
        OtherObject second = new OtherObject;
    }
}
孤云独去闲 2024-09-25 23:19:41

在某些情况下,您可以使用继承:

public class MyList<T1, T2> : List<Tuple<IEnumerable<HashSet<T1>>, IComparable<T2>>> { }

public void Meth()
{
    var x = new MyList<int, bool>();
}

In some cases you can go with inheritance:

public class MyList<T1, T2> : List<Tuple<IEnumerable<HashSet<T1>>, IComparable<T2>>> { }

public void Meth()
{
    var x = new MyList<int, bool>();
}
你的呼吸 2024-09-25 23:19:41

您可以为类添加别名:

using Test = NameSpace.MyClass;

仅当该类不是通用类时。

You can alias a class doing :

using Test = NameSpace.MyClass;

Only if the class is NOT generic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文