title: Using child visitor in C#
body: <p>I am setting up a testing component and trying to keep it generic.  I want to use a generic Visitor class, but not sure about using descendant classes.  </p>
  
  <p><strong>Example:</strong>  </p>
  
  <pre><code>public interface Interface_Test_Case
  {
    void execute();
    void accept(Interface_Test_Visitor v);
  }
  
  
  public interface Interface_Test_Visitor
  {
    void visit(Interface_Test_Case tc);
  }
  
  
  public interface Interface_Read_Test_Case
    : Interface_Test_Case
  {
    uint read_value();
  }
  
  
  public class USB_Read_Test
    : Interface_Read_Test_Case
  {
    void execute()
    { Console.WriteLine("Executing USB Read Test Case."); }
  
    void accept(Interface_Test_Visitor v)
    { Console.WriteLine("Accepting visitor."); }
  
    uint read_value()
    {
      Console.WriteLine("Reading value from USB");
      return 0;
    }
  }
  
  
  public class USB_Read_Visitor
    : Interface_Test_Visitor
  {
    void visit(Interface_Test_Case tc)
    { Console.WriteLine("Not supported Test Case."); }
  
    void visit(Interface_Read_Test_Case rtc)
    { Console.WriteLine("Not supported Read Test Case."); }
  
    void visit(USB_Read_Test urt)
    { Console.WriteLine("Yay, visiting USB Read Test case."); }
  }
  
  // Code fragment
    USB_Read_Test test_case;
    USB_Read_Visitor visitor;
    test_case.accept(visitor);
  </code></pre>
  
  <p>What are the rules the C# compiler uses to determine which of the methods in <code>USB_Read_Visitor</code> will be executed by the code fragment?</p>
  
  <p>I'm trying to factor out dependencies of my testing component.  Unfortunately, my current Visitor class contains <code>visit</code> methods for classes not related to the testing component.  Am I trying to achieve the impossible?</p>