public static string GetExceptionMethodAddress(Exception ee) { if (!(ee is NullReferenceException)) { return(""); } //获取调用堆栈 StackTrace trace = new StackTrace(ee, true); StackFrame frame = trace.GetFrame(0); int offset = frame.GetILOffset(); byte[] il = frame.GetMethod().GetMethodBody().GetILAsByteArray(); //获取调用指令 offset++; ushort instruction = il[offset++]; //打开潘多拉魔盒 ILGlobals global = new ILGlobals(); //翻译 OpCode code = OpCodes.Nop; if (instruction != 0xfe) { code = global.SingleByteOpCodes[(int)instruction]; } else { instruction = il[offset++]; code = global.MultiByteOpCodes[(int)instruction]; instruction = (ushort)(instruction | 0xfe00); } //获取方法信息 int metadataToken = NullReferenceHelper.ReadInt32(il, ref offset); MethodBase callmethod = frame.GetMethod().Module.ResolveMethod(metadataToken, frame.GetMethod().DeclaringType.GetGenericArguments(), frame.GetMethod().GetGenericArguments()); return(callmethod.DeclaringType + "." + callmethod.Name); }
public static string GetExceptionMethodAddress(Exception ee) { if (!(ee is NullReferenceException)) { return ""; } //获取调用堆栈 StackTrace trace = new StackTrace(ee, true); StackFrame frame = trace.GetFrame(0); int offset = frame.GetILOffset(); byte[] il = frame.GetMethod().GetMethodBody().GetILAsByteArray(); //获取调用指令 offset++; ushort instruction = il[offset++]; //打开潘多拉魔盒 ILGlobals global = new ILGlobals(); //翻译 OpCode code = OpCodes.Nop; if (instruction != 0xfe) { code = global.SingleByteOpCodes[(int)instruction]; } else { instruction = il[offset++]; code = global.MultiByteOpCodes[(int)instruction]; instruction = (ushort)(instruction | 0xfe00); } //获取方法信息 int metadataToken = NullReferenceHelper.ReadInt32(il, ref offset); MethodBase callmethod = frame.GetMethod().Module.ResolveMethod(metadataToken, frame.GetMethod().DeclaringType.GetGenericArguments(), frame.GetMethod().GetGenericArguments()); return callmethod.DeclaringType + "." + callmethod.Name; }