Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the class without instantiating object or having a static method?

I hate to see the name of the class used as a string parameter like "FileDownloader" in the code, and I would like to use something like this FileDownloader.Name(), where FileDownloader is name of the class.
Only problem is that I can't find out how to do that without instantiating object or creating a static method...

Is there a way to get a class name in .net without having the object instance and without creating a static method that returns the name of the class?

like image 981
This is it Avatar asked Feb 22 '11 11:02

This is it


People also ask

Can static methods be called without instantiating the class first?

In the same way that a static variable is associated with the class as a whole, so is a static method. In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object.

Which methods can be used without instantiating an object?

Static Method Static methods are the methods in Java that can be called without creating an object of class.

What type of methods can be called without instantiating an object first?

In a Web API, a static method is one which is defined by an interface but can be called without instantiating an object of that type first.

How do you access a static method of a class without creating an object for that class?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.


3 Answers

Sure:

var name = typeof(FileDownloader).Name;
like image 187
Anton Gogolev Avatar answered Sep 28 '22 07:09

Anton Gogolev


use the typeof operator:

typeof ( FileDownloader).Name
like image 38
Nick Avatar answered Sep 28 '22 06:09

Nick


Try typeof(YourClass).name. This should expose the name of your class

like image 45
Morten Avatar answered Sep 28 '22 07:09

Morten