Possible Duplicate:
Get type name without full namespace in C#
Does anybody know how we can get the name of a class in a simple way like what Java can do via class.getName() ?
NB: Yes, there are some messy workaround like CreateInstance of the class type and then get the name of object but I want a simple way.
Edit 1:
Actually, I need the class name in a method say X. Meanwhile, I don’t want to loose type-safety and I’m not gonna allow other developers to pass a string to the method. Something like this:
void X( ??? class) // --> don't know how
{
var className = get the name of class // --> which I don't know how
Console.WriteLine(className);
}
X(tblEmployee); //--> usage of X, where tblEmployee is a POCO class
You mean, like this?
To clarify, in .NET framework there’s a class named
Type. This class has a property namedNamethat retrieves the name of the class.So, to retrieve the Type of a class in compile time you can use
typeof.If you doesn’t know the type at runtime, you can retrieve it with the
GetType()method. This is common for all .NET objects.Answer for Edit 1
You need to pass a
TypeparameterAnd a call to
Xshould be like thisor