如何解决.NET中两个命名空间之间的引用歧义。

huangapple go评论61阅读模式
英文:

how to solve ambiguous reference between two namespaces in .NET

问题

我在Unity中使用.NET Framework进行开发。

我经常使用 List<string> 。突然,今天我收到消息说 zenject.List<t>System.Collections.Generic.List<t> 之间存在歧义。

当我点击 "yes" 时,它将所有 List<string> 实例更改为 System.Collections.Generic.List<string>

我不想使用Zenject的列表。我宁愿不每次需要列表时都写完整的 System.Collections.Generic.List<string>

是否有配置所有 List<string> 默认关联到泛型命名空间的选项?

我尝试写:

using List = System.Collections.Generic;

但它没有解决问题。

英文:

I'm developing in Unity with the .NET Framework.

I'm using List&lt;string&gt; a lot. Suddenly, today I got message that there is ambiguous between zenject.List&lt;t&gt; and System.Collections.Generic.List&lt;t&gt;.

When I clicked "yes", it changed all List&lt;string&gt; instances to System.Collections.Generic.List&lt;string&gt;.

I don't want to use list of Zenject at all. And I prefer not to write the full System.Collections.Generic.List&lt;string&gt; every time I need a list.

Is there an option to configure that all List&lt;string&gt; will be related to the generic namespace by default?

I tried to write

using List = System.Collections.Generic;

but it didn't solve the issue.

答案1

得分: 1

Your using alias misses a type in its declaration:

使用别名缺少类型声明:

using GenericListOfStrings = System.Collections.Generic.List<string>;
使用 GenericListOfStrings = System.Collections.Generic.List<string>;

I am not familiar with Zenject, but the better approach IMO would be to encapsulate all usages of Zenject in a class, so you don't need that namespace in your source code:

我不熟悉Zenject,但在我看来,更好的方法是将Zenject的所有用法封装在一个类中,这样你的源代码中就不需要这个命名空间:

For example:

例如:

using Zenject;

namespace Zenject // imagine this code in a third party library
{
class SomeZenjectClass { }
}

namespace YourCode
{
class ZenjectContainer
{
public void SomeMethod()
{
var obj = new SomeZenjectClass();
}
}
}

Then your code can exclude conflicting namespace:

然后你的代码可以排除冲突的命名空间:

using YourCode;

var zenject = new ZenjectContainer();
var list = new List<string>(); // I can use lists!
var zenject = new ZenjectContainer();
var list = new List<string>(); //我可以使用列表!

英文:

Your using alias misses a type in its declaration

using GenericListOfStrings = System.Collections.Generic.List&lt;string&gt;;

var strings = new GenericListOfStrings();

I am not familiar with Zenject, but the better approach IMO would be to encapsulate all usages of Zenject in a class, so you don't need that namespace in your source code

For example,

using Zenject;

namespace Zenject // imagine this code in a third party library
{
    class SomeZenjectClass { }
}

namespace YourCode
{
    class ZenjectContainer
    {
        public void SomeMethod()
        {
            var obj = new SomeZenjectClass();
        }
    }
}

Then your code can exclude conflicting namespace

using YourCode;

var zenject = new ZenjectContainer();
var list = new List&lt;string&gt;(); // I can use lists!

huangapple
  • 本文由 发表于 2023年5月7日 04:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定