Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-safely pass a class name to a method

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
like image 722
Alireza Avatar asked Dec 28 '12 13:12

Alireza


People also ask

Can we pass class name as parameter in Java?

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.

Can we call methods using class name?

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.

How to create class name in php?

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]*$ .

Can a class be a parameter?

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.

How to pass the type of a class as a parameter?

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...

How to get the class of an object with a type?

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".

What does it mean to be type safe in Java?

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.

What is the difference between a class and a type?

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.


3 Answers

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));
like image 166
Rotem Avatar answered Sep 22 '22 19:09

Rotem


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.

like image 20
Grant Thomas Avatar answered Sep 21 '22 19:09

Grant Thomas


void X(Type type)
{
    if(type == typeof(DesiredType))
    {
          Do Some Action
    }
}
like image 37
Gaurav Avatar answered Sep 24 '22 19:09

Gaurav