1

I'm trying to access the base implementation of a method from it's derived class when that class is being passed as a parameter to a static method. In the example below I would like the console to write "This is Parent..." when Child.WriteSomthing() is called.

As it stands it calls Child.WriteSomthing() recursively. Is there any way to achieve this?

public class Parent
{
     public virtual void WriteSomething()
     {
          Console.WriteLine("This is Parent...");
     }

public class Child : Parent
{
     public override void WriteSomething()
     {
          Helpers.SomeHelper(3, this);
     }
}

public static class Helpers
{
     public static void SomeHelper(int i, Child child)
     {
          switch (i)
          {
               case 1:
               Console.WriteLine("Child 1...");
               break;
               case 2: 
               Console.WriteLine("Child 2...");
               break;
               default: (Parent)child.WriteSomething();
          }
     }
}
JimmyB
  • 195
  • 1
  • 10

0 Answers0