19 Ekim 2013 Cumartesi

Get Calling Method using Reflection [C#]

Sponsorlu Bağlantılar
To get name of calling method use method StackTrace.Get­Frame. Create new instance ofStackTrace and call method GetFrame(1). The parameter is index of method call in call stack. Index of the first (the nearest) method call is „1“, so it returns a StackFrame of the calling method (method which directly called the current method). To get the method name useStackFrame.Get­Method (to get MethodBase) and then just get value of Name property.
Following example shows how to get calling method name. This can be useful e.g. to write debugging logs.
[C#]
using System.Diagnostics;

// get call stack
StackTrace stackTrace = new StackTrace();

// get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);

Sponsorlu Bağlantılar

Hiç yorum yok:

Yorum Gönder