如果您的程序集身份/命名空间为 Library.Testing
.
然后您创建了另一个程序集,其身份/ namespace 为 Library.Testing.One
Library.Testing.One
项目引用 Library.Testing
.
为什么你必须使用 using Library.Testing;
在你的类里面 Library.Testing.One
访问 Library.Testing
中的任何内容?
示例 1:
using System;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
这不起作用我得到两个异常
Warning 1 Load of property 'RootNamespace' failed. The string for the root namespace must be a valid identifier. Error 2 The type or namespace name 'BooBase' does not exist in the namespace 'Library.Testing.One.Library.Testing' (are you missing an assembly reference?)
例子2:
using System;
using Library.Testing;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
这行得通!
最佳答案
为 Library.Testing.One
添加“using”不会自动将 Library
和 Library.Testing
中的所有内容纳入范围。命名空间似乎是分层的这一事实可能是导致您混淆的原因。
例如,考虑将 using System.Data.SqlClient
添加到文件中。这不会自动将 System
和 System.Data
中的所有内容都纳入范围。
关于c# - 程序集访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589046/