کدام سلسله متدها، متد جاری را فراخوانی کردهاند؟
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۷/۰۶ ۹:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public void ScalarExecuted(DbCommand command,
DbCommandInterceptionContext<object> interceptionContext)
{
} public static string GetCallingMethodInfo()
{
var stackTrace = new StackTrace(true);
var frameCount = stackTrace.FrameCount;
var info = new StringBuilder();
var prefix = "-- ";
for (var i = frameCount - 1; i >= 0; i--)
{
var frame = stackTrace.GetFrame(i);
var methodInfo = getStackFrameInfo(frame);
if (string.IsNullOrWhiteSpace(methodInfo))
continue;
info.AppendLine(prefix + methodInfo);
prefix = "-" + prefix;
}
return info.ToString();
} private static string getStackFrameInfo(StackFrame stackFrame)
{
if (stackFrame == null)
return string.Empty;
var method = stackFrame.GetMethod();
if (method == null)
return string.Empty;
if (isFromCurrentAsm(method) || isMicrosoftType(method))
{
return string.Empty;
}
var methodSignature = method.ToString();
var lineNumber = stackFrame.GetFileLineNumber();
var filePath = stackFrame.GetFileName();
var fileLine = string.Empty;
if (!string.IsNullOrEmpty(filePath))
{
var fileName = Path.GetFileName(filePath);
fileLine = string.Format("[File={0}, Line={1}]", fileName, lineNumber);
}
var methodSignatureFull = string.Format("{0} {1}", methodSignature, fileLine);
return methodSignatureFull;
} Void Main(System.String[]) [File=Program.cs, Line=28]
-- Void Main(System.String[]) [File=Program.cs, Line=28] --- Void disposedContext() [File=Program.cs, Line=76] ---- Void Opened(System.Data.Common.DbConnection, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext) [File=DatabaseInterceptor.cs,Line=157]
private static bool isFromCurrentAsm(MethodBase method)
{
return method.ReflectedType == typeof(CallingMethod);
}
private static bool isMicrosoftType(MethodBase method)
{
if (method.ReflectedType == null)
return false;
return method.ReflectedType.FullName.StartsWith("System.") ||
method.ReflectedType.FullName.StartsWith("Microsoft.");
}