Click here to Skip to main content
16,007,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to get the name of the currently executing function in C#?
Posted
Updated 25-Aug-10 7:49am
v2

In what context ? You'd be IN the function in question, so, I don't see how you'd need to find that out, unless you're trying to write some sort of macro. In that case, the answer is no, you can't write macros in C# that would work the way C++ did to find the current line number, etc.
 
Share this answer
 
Comments
Colorbyte-OMD 23-Aug-10 17:17pm    
Dear Christian Graus
The context is in errorhandling. My whish is to get the name of the procedure or function that caused the error or exception. The exception class doesn't always return the name my function in the E.Source property.
Christian Graus 23-Aug-10 17:41pm    
You're saying the stack trace sometimes doesn't include the method that was being called ? I've never seen that happen.
Colorbyte-OMD 23-Aug-10 18:18pm    
Dear Christian Graus
No. I am saying that the E.Source property doesn't always return the name my function. The E.Stacktrace property does. For the moment I will continue to use the stacktrace. Thanks for your kind and polite answers.
 
Share this answer
 
Comments
AspDotNetDev 23-Aug-10 17:38pm    
Reason for my vote of 5
Was just about to suggest Environment.StackTrace.
This is part of something I used in a logging component in a similar situation, although I believe it requires the pdb file to be present. It doesn't trace back the exception like you're doing, but it is a nice way to instrument the code.
C#
public static void DebugWriteTrace()
{
    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(true);
    string f1info, f2info;
    GetTraceInfo(trace, out f1info, out f2info);
    Write(0, f1info + f2info);
}
private static void GetTraceInfo(StackTrace trace, out string f1, out string f2)
{
    System.Diagnostics.StackFrame sf = trace.GetFrame(1); //Gets caller's info
    int lineNo = sf.GetFileLineNumber();
    f1 = System.IO.Path.GetFileNameWithoutExtension(sf.GetFileName())
        + ":" + sf.GetMethod().Name 
        + ((lineNo != 0) ? ", Ln " + lineNo.ToString() : "");
    f2 = string.Empty;
    if (trace.FrameCount > 2)
    {
        sf = trace.GetFrame(2);
        lineNo = sf.GetFileLineNumber();
        f2 = "; called from " + sf.GetMethod().Name 
            + ((lineNo != 0) ? ", Ln " + lineNo.ToString() : "");
    }
}

There were overloads for the DebugWriteTrace, which is why GetTraceInfo is separate and returns two strings instead of one.
 
Share this answer
 
v2
Comments
Colorbyte-OMD 26-Aug-10 3:39am    
Dear John
Thanks for your answer, looks as an interesting way to instrument my code.
Greetings from Denmark

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900