Question

Consider the following code:

class A
{
    public void Foo()
    {
        string thisFunction = // get the name of the executing function (Foo)
        string thisType = // get the name of the object type (A)
        Log.PrintLog(thisFunction, thisType);
    }
}

public static class Log
{
    public static void PrintLog(string function, string type)
    {
        Console.WriteLine("Call from function {0}, type {1}", function, type);
    }
}

How can I find name of the executing function and object type? Any other solution except using [CallerFilePath] and [CallerMemberName]?

Was it helpful?

Solution 3

Generally you can create a StackTrace anywhere you want:

StackTrace st = new StackTrace();

Then it's just about cycling through the StackFrames:

StackFrame[] sf = st.GetFrames();

// in case your PrintLog has overloads or recursions
//  it may appear several times in the stack
string ownName = "PrintLog"; 
MethodInfo curMethod = null;
for(int i = 0; i < sf.Length; i++) {
     curMethod = sf[i].GetMethod();
     if(ownName != curMethod.Name)
         break;
}
Console.WriteLine("Call from function {0}, type {1}", 
                  curMethod.Name, 
                  curMethod.DeclaringType.Name);

You could even identify the class and parameters (you have the MethodInfo object to play around with)

Personally I put the Time TID caller class+type+name into my logging context.


As I understand you wanted to parse the function name and type to the PrintLog function. Thats just messy!

The beauty of this approach is, that you don't have to do that. You can retreive the name and type of the caller within the PrintLog method.

OTHER TIPS

 string thisFunction = Reflection.MethodBase.GetCurrentMethod().Name; 
 string thisType = this.GetType().Name;

Int .Net4.5 you have the CallerMemberNameAttribute

Allows you to obtain the method or property name of the caller to the method.

Also you may try with:

new StackFrame(1).GetMethod().Name;

Also check this interesting link answer:- Can you use reflection to find the name of the currently executing method?

If you can live with the path rather than the type name:

class A
{
    public void Foo()
    {
        Log.PrintLog();
    }
}

public static class Log
{
    public static void PrintLog([CallerMemberName] string function = null,
                                [CallerFilePath] string path = null)
    {
        Console.WriteLine("Call from function {0}, file {1}", function, path);
    }
}

For instance methods you might choose to use GetType() to obtain the current type - this is not possible for static methods, though. So you could use:

public static void PrintLog(object obj = null,
       [CallerMemberName] string function = null,
       [CallerFilePath] string path = null)
{
    string name = obj == null ? path : obj.GetType().Name;
    Console.WriteLine("Call from function {0}, name {1}", function, name);
}

with:

Log.PrintLog(this);

As explained here you may use MethodInfoclass to find return type as MethodInfo.ReturnType

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top