/// <summary> /// Gets the list of DynamicStackFrames for the current exception. /// </summary> internal static DynamicStackFrame[] GetDynamicStackFrames(Exception e) { List <DynamicStackFrame> frames = e.GetFrameList(); if (frames == null) { return(new DynamicStackFrame[0]); } frames = new List <DynamicStackFrame>(frames); List <DynamicStackFrame> identified = new List <DynamicStackFrame>(); // merge .NET frames w/ any dynamic frames that we have try { StackTrace outermostTrace = new StackTrace(e, false); IList <StackTrace> otherTraces = ExceptionHelpers.GetExceptionStackTraces(e) ?? new List <StackTrace>(); List <StackFrame> clrFrames = new List <StackFrame>(); foreach (StackTrace trace in otherTraces) { clrFrames.AddRange(trace.GetFrames() ?? new StackFrame[0]); // rare, sometimes GetFrames returns null } clrFrames.AddRange(outermostTrace.GetFrames() ?? new StackFrame[0]); // rare, sometimes GetFrames returns null int lastFound = 0; foreach (StackFrame clrFrame in InterpretedFrame.GroupStackFrames(clrFrames)) { MethodBase method = clrFrame.GetMethod(); for (int j = lastFound; j < frames.Count; j++) { MethodBase other = frames[j].GetMethod(); // method info's don't always compare equal, check based // upon name/module/declaring type which will always be a correct // check for dynamic methods. if (MethodsMatch(method, other)) { identified.Add(frames[j]); frames.RemoveAt(j); lastFound = j; break; } } } } catch (MemberAccessException) { // can't access new StackTrace(e) due to security } // combine identified and any remaining frames we couldn't find // this is equivalent of adding remaining frames in front of identified frames.AddRange(identified); return(frames.ToArray()); }