Blankman Blankman - 3 months ago 6
C# Question

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

My method looks like:

public string DoObjectProperties<T>(T obj, string text)
{

}


Now from within the method, I need to get the string value of the class name that i pass into the method's 'obj' parameter.

So if I pass in the User object, I need the text 'user'.

To get the properties I am using:
typeof(T).GetProperties()


How can I get the classes name?

Answer

Just use .Name like this:

typeof(T).Name

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