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?
You can see that in the main method, or in any instance method, I am capable to get the name of the generics type, in this case the main will print: java. lang. Integer. ...and you get the name of the subclass (if any).
A Generic class simply means that the items or functions in that class can be generalized with the parameter(example T) to specify that we can add any type as a parameter in place of T like Integer, Character, String, Double or any other user-defined type.
You cannot inherit a generic type. // class Derived20 : T {}// NO!
A generic class or structure can contain nongeneric procedures, and a nongeneric class, structure, or module can contain generic procedures. A generic procedure can use its type parameters in its normal parameter list, in its return type if it has one, and in its procedure code.
Just use .Name
like this:
typeof(T).Name
This gives for example "String", there's also .FullName
which would give "System.String"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With