Get the nameof supplied generic type in a generic interface

Viewed 60

Suppose having the following interface:

public interface ITraceManager<TManager> where TManager : class
    {
        string Name { get; }
        void DisplayName();
    }

A concrete class implementing this interface looks like the following:

public sealed class TraceManager<TManager> : ITraceManager<TManager> where TManager : class
    {
        public string Name => $"Foo Bar - {nameof(TManager)}";        

        public void DisplayName()
        {
            Console.WriteLine($"{Name}");            
        }
    }

I can register this dependency by constructor like the following:

readonly ITraceManager<LoginManager> _traceManager;
public LoginManager(ITraceManager<LoginManager> traceManager)
{
   _traceManager = traceManager;
}

when invoking the _traceManager.DisplayName() method, I'd expect the Name property to retain the value of nameof(LoginManager) since that is the supplied type. However, instead, the value of Name property is TManager.

What am I doing wrong?

0 Answers
Related