I need the class name in a method say for example X. Meanwhile, I don't want to loose type-safety and I'm not gonna allow other developers to pass a string (class name) 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
The Class class methods are widely used in Reflection API. The above statement creates the Class object for the class passed as a String argument(className). Note that the parameter className must be fully qualified name of the desired class for which Class object is to be created.
We can call a static method by using the ClassName. methodName. The best example of the static method is the main() method. It is called without creating the object.
The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ .
A class parameter defines a special constant value available to all objects of a given class. When you create a class definition (or at any point before compilation), you can set the values for its class parameters.
He wants to pass the type of a class (which resembles to ClassName.class or someObject.getClass (); so an object of type Class<T>). In Java, how can you pass the Type of a class as a parameter of a method ? It works! Just need to Extends the ParseObject class like so: public <T extends ParseObject> void fetchObjectList...
We have already seen, that applying "type" to an object returns the class of which the object is an instance of: If you apply type on the name of a class itself, you get the class "type" returned. <class 'list'> <class 'str'> <class 'type'> <class 'type'> A user-defined class (or the class "object") is an instance of the class "type".
When we are saying type-safe means we should not have to use the object data type. Here in our example, we need to use the data type as an integer. So at the time compilation, if we pass any data other than integer then it should give us a compile-time error. Let us see how to achieve this step by step.
A user-defined class (or the class "object") is an instance of the class "type". So, we can see, that classes are created from type. In Python3 there is no difference between "classes" and "types". They are in most cases used as synonyms. The fact that classes are instances of a class "type" allows us to program metaclasses.
What you are looking for is a called a Type
, which contains metadata about classes.
You can use the typeof(class)
or .GetType()
method on any object
instance.
The different is that typeof
is resolved statically and GetType
is resolved at runtime.
void X(Type type)
{
Console.WriteLine(type.FullName);
}
X(typeof(tblEmployee));
You could use generics and the FullName
property of Type
, as such:
void WriteClassName<TClass>(TClass item)
where TClass : class {
Console.WriteLine(item.GetType().FullName);
}
And then apply contraints on TClass
as per your requirements.
void X(Type type)
{
if(type == typeof(DesiredType))
{
Do Some Action
}
}
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