how to I get the class name when I am passing in a generic in my method?

obj.GetType().Name;
//Returns "ObjectName"

Works if your method is deep into a generic method chain, as long as obj was defined at the top of the chain.

typeof(T).Name;
//Returns "T" if the method calling it also is generic.

This only works if the calling method defined the Class of T.


typeof(T).Name?


Just use .Name like this:

typeof(T).Name

This gives for example "String", there's also .FullName which would give "System.String"

Tags:

C#

Reflection