Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

.net - Why can one aliased C# Type not be accessed by another?

good stuff

// 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>>;

bad stuff

// However **error** to alias another alias
using AliasListOfStringList2 = System.Collections.Generic.List<AliasStringList>;

Produces the compile error

The type or namespace name 'AliasStringList' could not be found (are you missing a using directive or an assembly reference?)

Note: this is the using directive not the using statement.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

From MSDN

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...