Why can nameof not be used with alias-qualified types at the root level?

Answer to underlying question

if you came here for compile-time desision – there is no way at this moment. It is an expected behavior from compilator side as result of expression compilation.

Quick runtime solution

(typeof(f::SomeCustomClassName)).Name instead of nameof(f::SomeCustomClassName)

Explanation

Now lets see again to nameof_expression (link)

nameof_expression = 'nameof' '(' named_entity ')'

where named_entity is

named_entity = named_entity_target ('.' identifier type_argument_list?)*

And lets look again to calling class name.
In common variant without aliases we got:
nameof(SomeCustomClassName) that unfolds to nameof(ConsoleApp1.SomeCustomClassName)

That matches named_entity_target.identifier.

But the alias nameof(f::SomeCustomClassName)

From specification

named_entity_target
    : simple_name
    | 'this'
    | 'base'
    | predefined_type 
    | qualified_alias_member
    ;

Is qualified_alias_member so named_entity_target = qualified_alias_member

So nameof_expression unfolds to

qualified_alias_member ('.' identifier type_argument_list?)*

Lets have a look to qualified_alias_member (link):

qualified_alias_member
    : identifier '::' identifier type_argument_list?
    ;

According to all of this we have

identifier '::' identifier ('.' identifier type_argument_list?)*

That does not match to nameof(f::SomeCustomClassName)
Thats why it is incorrect for compilator.
Thats why it need for dot
nameof(f::SomeCustomClassName.SomeCustomClassName2)

it is not a bug. It is miscalculation from expression creators or porposive limitation.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)