为什么可以在一个别名C#类型不被其他访问? [英] Why can one aliased C# Type not be accessed by another?
问题描述
好东西
// ok to alias a List Type
using AliasStringList = System.Collections.Generic.List<string>;
// and ok to alias a List of Lists like this
using AliasListOfStringList1 = System.Collections.Generic.List<System.Collections.Generic.List<string>>;
坏的东西
// However **error** to alias another alias
using AliasListOfStringList2 = System.Collections.Generic.List<AliasStringList>;
产生编译错误
Produces the compile error
的类型或命名空间名称 AliasStringList找不到 (是否缺少using指令或 程序集引用?)的
The type or namespace name 'AliasStringList' could not be found (are you missing a using directive or an assembly reference?)
请注意:这是使用指令< /一>不是 using语句
推荐答案
您就不能使用使用其他使用里面声明的别名。对于一组usings像你有,你可以使用声明不存在假设的兄弟姐妹。
You just can't use an alias declared in a using inside another using. For a set of usings like you have, you can assume that sibling using declarations don't exist.
在其中的使用别名指令的顺序的写并不重要,而命名空间或类型名称的解析的通过的使用引用-alias-指令的是不会受的使用别名指示的本身或其他using指令中直接包含编译单元或命名空间体。换句话说,在命名空间或类型名称的的的使用别名指示的解析为,如果直接包含编译单元或命名空间体中没有使用,指令
The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives
它提供了您的具体问题的简单的例子,这是预期的行为:
It provides simplified example of your exact problem, this is expected behavior:
namespace N1.N2 {}
namespace N3
{
using R1 = N1; // OK
using R2 = N1.N2; // OK
using R3 = R1.N2; // Error, R1 unknown
}
这篇关于为什么可以在一个别名C#类型不被其他访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!