简体   繁体   English

C# 嵌套类型别名 - 无法解析符号

[英]C# Nested Type Aliases - Cannot Resolve Symbol

Why doesn't this compile in Unity 2018 C#?为什么这不能在 Unity 2018 C# 中编译?

namespace MyNS
{
    using FooTable = Dictionary<int, float[]>;
    using BarTable = Dictionary<int, FooTable>;
} 

Likewise, if I omit the namespace:同样,如果我省略名称空间:

using FooTable = System.Collections.Generic.Dictionary<int, float[]>;
using BarTable = System.Collections.Generic.Dictionary<int, FooTable>;

I get the same compiler error.我得到相同的编译器错误。

(Frankly I don't know why the Dictionary qualifiers are optional inside the namespace, yet mandatory outside!) (坦率地说,我不知道为什么 Dictionary 限定符在名称空间内是可选的,但在外部是强制性的!)

The compiler error is for the last symbol in the using BarTable... line: "Cannot resolve symbol FooTable ".编译器错误是针对using BarTable...行中的最后一个符号:“无法解析符号FooTable ”。

Is it not possible to nest type aliases in this way?这样嵌套类型别名不就可以了吗?

It is just not allowed by the compiler:编译器不允许这样做:

https://learn.microsoft.com/en-us/do.net/csharp/language-reference/keywords/using-directive#remarks https://learn.microsoft.com/en-us/do.net/csharp/language-reference/keywords/using-directive#remarks

Create a using alias directive to make it easier to qualify an identifier to a namespace or type.创建一个 using 别名指令,以便更轻松地将标识符限定为命名空间或类型。 In any using directive, the fully-qualified namespace or type must be used regardless of the using directives that come before it.在任何 using 指令中,必须使用完全限定的命名空间或类型,而不管它之前的 using 指令如何。 No using alias can be used in the declaration of a using directive.在 using 指令的声明中不能使用 using 别名。 For example, the following generates a compiler error:例如,以下生成编译器错误:

using s = System.Text;
using s.RegularExpressions; // Generates a compiler error.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM