2

我在名为 Hasher 的命名空间中有一个名为 Hasher 的类。因此,完全限定名称将是:

Hasher.Hasher ...

我正在尝试在外部程序集(C#)中使用 Hasher 类。我已将命名空间导入到我的类中:

using Hasher;

但是当我尝试使用 Hasher 类时,编译器将找不到它。

using Hasher;

namespace Test {
  ///<summary>
  ///This is a test class for HasherTest and is intended
  ///to contain all HasherTest Unit Tests
  ///</summary>
  [TestClass()]
  public class HasherTest {

    ///<summary>
    ///A test for GenerateFromRawData with null seed
    ///</summary>
    [TestMethod()]
    [ExpectedException( typeof( ArgumentNullException ) )]
    public void GenerateFromRawDataTest_NullSeed() {
      byte[] seed = null;
      byte[] salt = null;

      seed = null;
      salt = null;

      Hasher.GenerateFromRawData( seed, salt );
    }

}

生成:

Error   The type or namespace name 'GenerateFromRawData' does not exist in the namespace 'Hasher' (are you missing an assembly reference?)  M:\j41833b_UR403088_ReportingDotNet\ReportingDotNet\src\AG385\_UnitTest\HasherTest.cs   _UnitTest

我没有正确使用“使用”吗?(我的主要语言是 VB.NET,所以我的 C# 有点生疏。粗略检查 MSDN 文档并没有发现任何东西)

编辑:这很好用。

namespace Test {
  ///<summary>
  ///This is a test class for HasherTest and is intended
  ///to contain all HasherTest Unit Tests
  ///</summary>
  [TestClass()]
  public class HasherTest {

    ///<summary>
    ///A test for GenerateFromRawData with null seed
    ///</summary>
    [TestMethod()]
    [ExpectedException( typeof( ArgumentNullException ) )]
    public void GenerateFromRawDataTest_NullSeed() {
      byte[] seed = null;
      byte[] salt = null;

      seed = null;
      salt = null;

      Hasher.Hasher.GenerateFromRawData( seed, salt );
    }

}
4

1 回答 1

3

感谢@asawyer 提供以下文章:

http://blogs.msdn.com/b/ericlippert/archive/2010/03/09/do-not-name-a-class-the-same-as-its-namespace-part-one.aspx

有两个分析器。一、使用外部别名:

http://msdn.microsoft.com/en-us/library/ms173212(v=vs.100).aspx

二、重命名Hasher命名空间。(当您可以控制源代码并且这是我选择的选项时,建议您这样做。)

于 2012-06-12T17:50:15.413 回答