【问题标题】:Is there a way to have your own namespace named 'x.System' [duplicate]有没有办法让你自己的命名空间命名为'x.System'[重复]
【发布时间】:2014-07-26 02:08:28
【问题描述】:
namespace Company.Product.System
{
using System;
using System.Collections.Generic;
...

这会产生编译器错误,因为现在 Visual Studio 找不到 System.Collections.Generic;。有没有办法解决这个问题?我不想听到这是否是一个坏主意,这个决定来自我之上,我在这件事上几乎没有选择。

【问题讨论】:

  • 为什么不将 using 语句放在命名空间之外?
  • 名称空间先生上方的左括号有什么用? @cost
  • @ScottChamberlain 这是个好主意,很明显,但我没有想到。代码由工具生成

标签: c#


【解决方案1】:

你有两个选择,将 using 语句放在命名空间之外

using System;
using System.Collections.Generic;

namespace Company.Product.System
{
...

或者将global:: 前缀添加到命名空间,这会强制它使用根目录,而不是假设您要使用Company.Product.System.Collections

namespace Company.Product.System
{
using global::System;
using global::System.Collections.Generic;
...

【讨论】:

    【解决方案2】:

    要么将你的 using 指令移到命名空间块之外,要么像这样使用global namespace alias

    namespace Company.System
    {
        using global::System.Collections.Generic;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 2012-11-06
      • 1970-01-01
      • 2011-07-11
      • 2010-09-19
      • 2022-07-23
      相关资源
      最近更新 更多