If I have a class called MyProgram
, is there a way of retrieving "MyProgram" as a string?
Try this:
this.GetType().Name
-
39If you're in a static method then the developer knows what the name of the type is. You can just type it in as a string in the source code. – Eric Lippert Jan 22 '10 at 0:33
-
131@EricLippert: If you type the name in, the compiler won't catch it if the class is renamed. – Halvard Mar 14 '13 at 11:18
-
12@Halvard: First, if the static method is in the current type then the name is optional; if you're worried about it changing, omit it. Second, Visual Studio will automatically give you a smart tag when you rename a class that renames all instances of it. And third, if you're renaming a class then odds are good you're going to have to make a lot of changes in a lot of places already. – Eric Lippert Mar 14 '13 at 14:41
-
12@EricLippert You are right. Any new version of Visual Studio or ReSharper will catch the strings with the same name as the class being renamed. My previous comment is just some old, now useless knowledge ... – Halvard Mar 15 '13 at 9:24
-
21
I wanted to throw this up for good measure. I think the way @micahtan posted is preferred.
typeof(MyProgram).Name
-
27This is actually better, because: 1. It will work in static context 2. It is compile time computed, so it doesn't cost like reflection – Gilbert Dec 14 '13 at 15:38
-
6@JimBalter It has multiple advantages: 1. Static context. 2. The type portion will not be re-evaluated by the CLR each time - it will be written to the MSIL. 3. It protects you from someone declaring a new "GetType()". – Gilbert Apr 16 '14 at 19:48
-
11If you want to get inherited class name and this call is in the parent then it won't work. – Gh61 Jul 25 '14 at 9:33
-
12This has the disadvantage of having to reference the type explicitly, which makes it less easily reusable. – cprcrack Feb 16 '15 at 17:31
-
22
With C# 6.0, you can use the nameof
operator:
nameof(MyProgram)
-
4
Although micahtan's answer is good, it won't work in a static method. If you want to retrieve the name of the current type, this one should work everywhere:
string className = MethodBase.GetCurrentMethod().DeclaringType.Name;
-
2Nice catch, although I think my method is preferred in this case. – ChaosPandion Jan 21 '10 at 21:36
-
5This Won't work for non-virtual methods, as it will return the name of the type that the method is declared and implemented in, (possibly up the inheritance chain), not the concrete type of the instance you are actually executing the code from. – Charles Bretana Jan 21 '10 at 21:56
-
1This doesn't seem to work anymore in the DNX (Dot Net Execution) framework. They removed the GetCurrentMethod() method and left only GetMethodFromHandle(). – Astaar Sep 15 '15 at 10:12
-
This is exactly what I needed to get the name of the concrete class currently executing code called from a virtual function in a descendant. – DrFloyd5 Mar 25 '20 at 17:35
For reference, if you have a type that inherits from another you can also use
this.GetType().BaseType.Name
If you need this in derived classes, you can put that code in the base class:
protected string GetThisClassName() { return this.GetType().Name; }
Then, you can reach the name in the derived class. Returns derived class name. Of course, when using the new keyword "nameof", there will be no need like this variety acts.
Besides you can define this:
public static class Extension
{
public static string NameOf(this object o)
{
return o.GetType().Name;
}
}
And then use like this:
public class MyProgram
{
string thisClassName;
public MyProgram()
{
this.thisClassName = this.NameOf();
}
}
Use this
Let say Application Test.exe is running and function is foo() in form1 [basically it is class form1], then above code will generate below response.
string s1 = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
This will return .
s1 = "TEST.form1"
for function name:
string s1 = System.Reflection.MethodBase.GetCurrentMethod().Name;
will return
s1 = foo
Note if you want to use this in exception use :
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace );
}
-
DeclaringType is declared
[Nullable(2)]
so you get an warning when null check are active. – Martin Oct 11 '19 at 14:50 -
this
can be omitted. All you need to get the current class name is:
GetType().Name
Get Current class name of Asp.net
string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();
-
1DeclaringType is declared
[Nullable(2)]
so you get an warning when null check are active. – Martin Oct 11 '19 at 14:51