Click or drag to resize

Visitor Design Pattern

Examples of using the Visitor design pattern to process a code object tree.

Using the Visitor design pattern to process a code object tree
C#
// The Visitor design pattern is supported for processing code objects.
// Create a class that implements the IVisitor interface, and pass it to the Accept(IVisitor) method
// of the root object of a tree of code objects (such as a Solution), and all code objects in the
// tree will be visited, calling the appropriate method of the IVisitor interface.

// Here is a simple example that just counts the total objects (you may use the MyVisitor class as a
// template for your own class):
Solution solution = Solution.Load("Nova.Examples.sln");
if (solution != null)
{
    Log.WriteLine("Solution '" + solution.Name + "' successfully loaded, parsed, resolved.");
    var myVisitor = new MyVisitor();
    solution.Accept(myVisitor);
    Log.WriteLine("Total code objects in the solution: " + myVisitor.TotalObjects.ToString("N0"));
}