Take the 2-minute tour ×

I'm working a C# API that has a fairly complex order of virtual method calls that could be overridden in various levels of the type hierarchy. I'd like to be able to document the order of execution of these methods, and I am wondering if there's a way to do it without having to pepper the classes with Trace writes. Given that each of these classes has over 2k LOC, it would be a pretty monumental task to do by hand!

Let me try to give a quick example of a common method flow:

public class Base
    protected void Init()

    protected virtual void AfterInitInternal() { }
    protected virtual void OnInit() { }
    protected virtual void BeginInitInternal() { }

public class Derived : Base
    protected sealed override void BeginInitInternal()
        // Logic here

    protected sealed override void AfterInitInternal()
        // Logic here

    protected virtual void BeginInit() { }
    protected virtual void AfterInit() { }

public class Concrete : Derived
    protected override void BeginInit() { /* Logic here */ }
    protected override void OnInit() { /* Logic here */ }
    protected override void AfterInit() { /* Logic here */ }

The Concrete implementation is created by the consumer of the API, and I want to be able to inform him/her of the order of all of these calls (and ideally the context too, but that's outside the scope of the question.

I don't necessarily need a UML sequence diagram, but that's about the closest analog I've found to what I'm looking for.

Ideally, I'd like it to tell the tool to look at the implementation of Base.Init() on Derived and emit something like:

  1. Derived.BeginInit()
  2. Base.OnInit()
  3. Derived.AfterInit()

I've tried out the VS Sequence Diagram generator, but I can't seem to tell it to use Concrete but start at Base.Init(). I've also tried using the VS Code Map but it chokes on the complexity of the solution and generally errors out. Finally, I tried using NDepend, which comes close - it can tell me all of the calls, but not the order of them.

Is there anything else I could try, or should I just get started with pasting Trace.WriteLines everywhere?

share|improve this question
Take a look at the StackTrace class, it might offer what you need: msdn.microsoft.com/en-us/library/… –  Davio Dec 20 '12 at 14:02
That's interesting, but I don't think it would help since I'd still have to grab a trace for every virtual method. –  Rob H Dec 20 '12 at 16:33
Might not save you from using trace statements, but perhaps you could employ something like Aspect Oriented Programming (for example, PostSharp) with which you can adorn the methods with reusable tracing/logging information and let you quickly modify it as desired in one place. EDIT: Maybe even combine it with Davio's suggestion of pulling a stack trace and crawling up the call stack. –  Chris Sinclair Jan 20 '13 at 3:23
I've hacked in a solution based on Davio's suggestion, by basically adding a ton debug writes of StackFrame(0) and StackFrame(1) to dump the info about the current method and its caller. It got the job done but I still feel like there has to be a better way. I like PostSharp but would rather not have to decorate every method with an attribute. –  Rob H Jan 22 '13 at 19:09
add comment

2 Answers

Oh. This is good case for using Roslyn.

If you want to extract custom information from your source code, that is dependent on that source code, without having the source adapt to your reader; I would say this is a strong case for using Roslyn.


share|improve this answer
add comment

You can consider intercepting calls to each method and can do whatever out of the box like logging, creating document etc.

You can see here how we can intercept calls of methods.


share|improve this answer
Interesting idea. Unfortunately, the application is not using IOC and I don't think I could hack it in within a reasonable amount of time. –  Rob H Jan 22 '13 at 18:52
add comment

Your Answer


