C# getting its own class name

C#Reflection

C# Problem Overview


If I have a class called MyProgram, is there a way of retrieving "MyProgram" as a string?

C# Solutions


Solution 1 - C#

Try this:

this.GetType().Name

Solution 2 - C#

I wanted to throw this up for good measure. I think the way @micahtan posted is preferred.

typeof(MyProgram).Name

Solution 3 - C#

With C# 6.0, you can use the nameof operator:

nameof(MyProgram)

Solution 4 - C#

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;

Solution 5 - C#

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();
    }
}

Solution 6 - C#

For reference, if you have a type that inherits from another you can also use

this.GetType().BaseType.Name

Solution 7 - C#

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 );
    
}

                    

Solution 8 - C#

this can be omitted. All you need to get the current class name is:

GetType().Name

Solution 9 - C#

Get Current class name of Asp.net

string CurrentClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name.ToString();

Solution 10 - C#

This can be used for a generic class

typeof(T).Name

Solution 11 - C#

The easiest way is to use the call name attribute. However, currently, there is no attribute class that returns the class name or the namespace of the calling method.

See: CallerMemberNameAttributeClass

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
//  message: Something happened.
//  member name: DoProcessing
//  source file path: c:\Users\username\Documents\Visual Studio 2012\Projects\CallerInfoCS\CallerInfoCS\Form1.cs
//  source line number: 31

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeltanovemberView Question on Stackoverflow
Solution 1 - C#micahtanView Answer on Stackoverflow
Solution 2 - C#ChaosPandionView Answer on Stackoverflow
Solution 3 - C#CyanfishView Answer on Stackoverflow
Solution 4 - C#Thomas LevesqueView Answer on Stackoverflow
Solution 5 - C#bafsarView Answer on Stackoverflow
Solution 6 - C#mikeschuldView Answer on Stackoverflow
Solution 7 - C#Harshal Doshi JainView Answer on Stackoverflow
Solution 8 - C#Mikael EngverView Answer on Stackoverflow
Solution 9 - C#Abbas aliView Answer on Stackoverflow
Solution 10 - C#Gade IfeoluwaView Answer on Stackoverflow
Solution 11 - C#Display nameView Answer on Stackoverflow