Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- // get call stack
- StackTrace stackTrace = new StackTrace();
- // get calling method name
- Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name);
- new StackFrame(1).GetMethod().Name;
- /// <summary>
- /// Returns the call that occurred just before the "GetCallingMethod".
- /// </summary>
- public static string GetCallingMethod()
- {
- return GetCallingMethod("GetCallingMethod");
- }
- /// <summary>
- /// Returns the call that occurred just before the the method specified.
- /// </summary>
- /// <param name="MethodAfter">The named method to see what happened just before it was called. (case sensitive)</param>
- /// <returns>The method name.</returns>
- public static string GetCallingMethod(string MethodAfter)
- {
- string str = "";
- try
- {
- StackTrace st = new StackTrace();
- StackFrame[] frames = st.GetFrames();
- for (int i = 0; i < st.FrameCount - 1; i++)
- {
- if (frames[i].GetMethod().Name.Equals(MethodAfter))
- {
- if (!frames[i + 1].GetMethod().Name.Equals(MethodAfter)) // ignores overloaded methods.
- {
- str = frames[i + 1].GetMethod().ReflectedType.FullName + "." + frames[i + 1].GetMethod().Name;
- break;
- }
- }
- }
- }
- catch (Exception) { ; }
- return str;
- }
- StackFrame frame = new StackFrame(1);
- frame.GetMethod().Name; //Gets the current method name
- MethodBase method = frame.GetMethod();
- method.DeclaringType.Name //Gets the current class name
- private static MethodBase GetCallingMethod()
- {
- return new StackFrame(2, false).GetMethod();
- }
- private static Type GetCallingType()
- {
- return new StackFrame(2, false).GetMethod().DeclaringType;
- }
Add Comment
Please, Sign In to add comment