Using child visitor in C#

Posted by Thomas Matthews on Stack Overflow See other posts from Stack Overflow or by Thomas Matthews
Published on 2011-03-12T00:04:44Z Indexed on 2011/03/12 0:10 UTC
Read the original article Hit count: 238

Filed under:
|
|

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.

Example:

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);

What are the rules the C# compiler uses to determine which of the methods in USB_Read_Visitor will be executed by the code fragment?

I'm trying to factor out dependencies of my testing component. Unfortunately, my current Visitor class contains visit methods for classes not related to the testing component. Am I trying to achieve the impossible?

© Stack Overflow or respective owner

Related posts about c#

Related posts about inheritance