How to get a class name without instantiating an object or static method?

I don’t like to see a class name that is used as a string parameter, for example “FileDownloader” in the code, and I would like to use something like this FileDownloader.Name (), where FileDownloader is the class name.
The only problem is that I cannot find out how to do this without instantiating an object or creating a static method ...

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

+5
source share
4 answers

Of course:

var name = typeof(FileDownloader).Name;
+13

typeof:

typeof ( FileDownloader).Name
+3

typeof (YourClass).name.

+1

VB.Net:

Dim sf As StackFrame = New StackFrame()
Dim mb As MethodBase = sf.GetMethod()
Dim className = mb.DeclaringType.Name

There you go. Totally dynamic. Just be careful with refactoring ... If you move it to another class, the class name will change ... :)

0
source

All Articles