问题描述
为什么这不能在 Unity 2018 C# 中编译?
namespace MyNS
{
using FooTable = Dictionary<int,float[]>;
using BarTable = Dictionary<int,FooTable>;
}
同样,如果我省略命名空间:
using FooTable = System.Collections.Generic.Dictionary<int,float[]>;
using BarTable = System.Collections.Generic.Dictionary<int,FooTable>;
我遇到了同样的编译器错误。
(坦率地说,我不知道为什么 Dictionary 限定符在命名空间内是可选的,而在外部却是强制性的!)
编译器错误是针对 using BarTable...
行中的最后一个符号:“无法解析符号 FooTable
”。
这种方式不能嵌套类型别名吗?
解决方法
只是编译器不允许:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#remarks
创建一个 using alias 指令,以便更容易地限定一个 命名空间或类型的标识符。在任何 using 指令中, 无论使用什么,都必须使用完全限定的命名空间或类型 在它之前的指令。不能使用别名 using 指令的声明。例如,以下生成 编译器错误:
using s = System.Text;
using s.RegularExpressions; // Generates a compiler error.