Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is performance hit by using Caller Information attributes?

I am trying to find ways to log method name in an efficient manner w.r.t. speed and maintainability. I guess, In .NET 4.5 Caller Information attributes are exposed just for this purpose except the speed part. I feel these are just syntactic sugars from using System.Reflection.MethodBase.GetCurrentMethod() or stackTrace.GetFrame(1).GetMethod().Name (Got from here). (or) Are these methods give performance benefits too?

In C#, Is there a way to get method name at compile time (as like in C++)?

like image 389
Imran Avatar asked Apr 19 '13 09:04

Imran


1 Answers

The caller information attributes cause the C# compiler to supply the caller's name at the callsite. This happens at compile-time, there is no reflection involved.

public void DoProcessing()
{
    LogCall();
}

public void LogCall([CallerMemberName] string memberName = "")
{
     Console.WriteLine(memberName + " was called.");
}

Will be compiled to:

public void DoProcessing()
{
    LogCall("DoProcessing");
}

public void LogCall(string memberName)
{
     Console.WriteLine(memberName + " was called.");
}
like image 114
Daniel Avatar answered Sep 23 '22 02:09

Daniel