Search code examples
c#genericsfully-qualified-naming

Get name of generic class without tilde


I am trying to get the type name of T using this:

typeof(T).Name

The name of the class is ConfigSettings

Instead of returning ConfigSettings it is returning ConfigSettings`1.

Is there any specific reason why? How can I return the actual name without `1?


Solution

  • The back-tick is indicative that the class is a generic type. Probably the easiest thing is to just lop off anything from the back-tick forward:

    string typeName = typeof(T).Name;
    if (typeName.Contains('`')) typeName = typeName.Substring(0, typeName.IndexOf("`"));