5

I have an alias within my source-code file like this:

MyClass.cs

using System;               
using SomeClass = NewClass;
    
public class Program
{
    public static void Main()
    {
        Console.WriteLine(nameof(SomeClass));
    }
}
public class NewClass { }

I need this because the names of my classes changed and now I need to compile for both the old and the new class-structure symultaneously.

When I run that code I get "SomeClass" but I' d expected "NewClass". Why doesn't nameof reflect the alias using the using-directive in this case?

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 6
    [By design](https://developercommunity.visualstudio.com/content/problem/1173546/nameoftypealias-should-return-type-name-instead-of.html)? – Sinatr Feb 05 '21 at 13:54
  • 3
    It strikes me that this is one of those situations where you can't always get what you want because as many people would expect it to reflect `SomeClass` as would expect `NewClass`. – Damien_The_Unbeliever Feb 05 '21 at 13:54
  • @Sinatr Yeap, seems so. Thanks for the link – MakePeaceGreatAgain Feb 05 '21 at 13:55
  • Note that `string str1 = nameof(System)` works, so `nameof` works even on things like namespaces, and `using K = System; string str2 = nameof(K)` will return `K` – xanatos Feb 05 '21 at 13:55
  • @Damien_The_Unbeliever Maybe you´re right. I hoped I can **avoid** naming-confusions within my code-migration project using `nameof`. However it seems I´m **causing** them instead. – MakePeaceGreatAgain Feb 05 '21 at 13:57
  • "I need this because the names of my classes changed and now I need to compile for both the old and the new class-structure symultaneously." Sounds exactly like a use case for the adapter pattern to me. https://en.wikipedia.org/wiki/Adapter_pattern . Especially if the business logic of the two systems is different. Maybe I'm missing some context though ;) – Dan Rayson Feb 05 '21 at 14:12
  • @DanRayson No, the BL keeps the same, only the names of the classes change. – MakePeaceGreatAgain Feb 05 '21 at 14:16

2 Answers2

3

It's because the nameof keyword is meant to "get the name of an identifier", without evaluating its value or anything else, like said in docs:

A nameof expression is evaluated at compile time and has no effect at run time.

More info here

LoRdPMN
  • 512
  • 1
  • 5
  • 18
  • But because it **is** a compile-time constant I´d expected the `using` which is also evaluated at compile-time to be reflected. – MakePeaceGreatAgain Feb 05 '21 at 13:54
  • Yes, one can expect it to behave like that, but unfortunately, as it is designed it gets its constant value before anything else on compile-time. – LoRdPMN Feb 05 '21 at 13:56
0

Your specific question was "why doesn't it?" but, here's a "what can do it instead?" answer for you :)

Use typeof instead of nameof.

using System;               
using SomeClass = NewClass;
    
public class Program
{
    public static void Main()
    {
        Console.WriteLine(typeof(SomeClass));
    }   
}

public class NewClass { }

prints

NewClass
Dan Rayson
  • 1,315
  • 1
  • 14
  • 37