Exemplo n.º 1
0
 public void ReflectiveVisit(VisitorElement element)
 {
     // Use reflection to find and invoke the correct Visit method
     Type[] types = new Type[] { element.GetType() };
     MethodInfo methodInfo = this.GetType().GetMethod("Visit", types);
     if (methodInfo != null)
         methodInfo.Invoke(this, new object[] { element });
     else Console.WriteLine("Unexpected Visit");
 }
Exemplo n.º 2
0
    static void Main()
    {
        // Set up the object structure
        VisitorElement objectStructure =
          new VisitorElement(
              new VisitorElement(
              new ElementWithLink(
               new VisitorElement(
                     new VisitorElement(
                       new ElementWithLink(
                   new VisitorElement(null),
                     new VisitorElement(
                     null)))),
          new VisitorElement(
              new VisitorElement(
              new VisitorElement(null))))));

        Console.WriteLine("Count the elements");
        CountVisitor visitor = new CountVisitor();
        visitor.CountElements(objectStructure);
        Console.WriteLine("Number of Elements is: " + visitor.Count);
    }
Exemplo n.º 3
0
 // Only Elements are counted
 public void Visit(VisitorElement element)
 {
     Count++;
 }
Exemplo n.º 4
0
 public void CountElements(VisitorElement element)
 {
     ReflectiveVisit(element);
     if (element.Part != null) CountElements(element.Part);
     if (element.Next != null) CountElements(element.Next);
 }
Exemplo n.º 5
0
 public ElementWithLink(VisitorElement part, VisitorElement next)
 {
     Next = next;
     Part = part;
 }
Exemplo n.º 6
0
 public VisitorElement(VisitorElement next)
 {
     Next = next;
 }