0

I would like to provide some helpful message to debug a particular exception and for that I need to access the instances of all the classes of the methods in the stack.

Let's take the following code as an example. Say I have the following class:

Public Class Foo
  Public Name As String
  Public Sub DoStuff(Optional N As Integer = 10)
    If N = 0 Then Throw New Exception("Boom")
    Dim C As New Foo
    C.Name = "Class level =  " & N
    C.DoStuff(N - 1)
  End Sub
End Class

It will cause an exception. What I want to do is to capture the values of Name of all the Foo instances in the stack.

Try
  Dim C As New Foo
  C.Name = "Base class"
  C.DoStuff()
Catch ex As Exception
  Dim St As New StackTrace(ex)
  For i As Integer = 0 To St.FrameCount - 1
    Dim M = St.GetFrame(i).GetMethod()
    Console.WriteLine(M.Name & " " & M.DeclaringType.ToString)
  Next
End Try

I am almost these, I can get the method and the type of the classes in the call stack, but what I am missing is to be able to get the actual instance in order to be able to access the field Name of each class Foo in the stack.

Is that even possible?

thanks Charles

2
  • If you're trying to debug something like this in the IDE, the System.Diagnostics.DebuggerDisplayAttribute may be useful.
    – Mark Hurd
    Feb 13, 2014 at 3:52
  • Thanks. That would be outside of Visual Studio though. This is for a sort of excel-like software. I can spot circular references easily, which generate an exception. What I am trying to do is to help the user find what the circular reference was by going through the stack of calculation trees.
    – Charles
    Feb 13, 2014 at 8:37

1 Answer 1

0

Duplicate of Is it possible to get parameters' values for each frame in call stack in .NET.

What would be needed is access to whole data stack & heap the of .NET process. I don't think it is easy (and even possible). What is recommended is use logging mechanism instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.