什么使特定的使用指令不必要?

时间:2018-01-22 11:14:52

标签: c#

我有一个包含多个项目的解决方案。

项目A具有名称空间1,该名称空间使用特定的类X并且具有使用指令。那个项目编译。

项目B具有类2(在命名空间2中),它使用相同的类X但不编译,因为“名称X在当前上下文中不存在”。如果我包含类1中的using指令,我会得到“使用指令是不必要的”

因此,项目A和B有一些不同之处,其中一个需要使用using指令而另一个则不需要。

我的解决方案明显存在多个问题,但首先我想了解是什么使得使用指令成为必要。我在哪里可以开始寻找?

已编辑以澄清上面的文字,并根据要求添加以下代码段。

项目A:

using Microsoft.WindowsAzure; //CloudConfigurationManager is here
using MyNamespace.MyClass; //Constants is here

namespace n1
{
   private static string resourceId = CloudConfigurationManager.GetSetting("id");
   Console.WriteLine($"{resourceId}{Constants.Blah}("blah")";
}

项目B:

using Microsoft.WindowsAzure; //directive unnecessary
using MyNamespace.MyClass; //directive unnecessary

namespace Testing
{
  class Program
  {
    public static void Main(string[] args)
    {
      string resourceId = CloudConfigurationManager.GetSetting("id"); // The name CloudConfigurationManager does not exist in the current context
      Console.WriteLine($"{resourceId}{Constants.Blah}("blah")"; // The name Constants does not exist in the current context
    }
  } 
}

1 个答案:

答案 0 :(得分:0)

您需要分别在项目B中添加using X类X.来自using X的{​​{1}}不会仅通过添加Project A来转发。

如果您只使用using A中的函数/类,它封装了Class 1的功能,那么只需引用class X即可。 但在你的情况下,据我所知,你想直接使用X功能 - 因此使用单独的using指令。

相关问题