How to determine which inheriting class is using an abstract class's methods.

Posted by Kin on Stack Overflow See other posts from Stack Overflow or by Kin
Published on 2010-04-18T08:58:57Z Indexed on 2010/04/18 9:23 UTC
Read the original article Hit count: 259

In my console application have an abstract Factory class "Listener" which contains code for listening and accepting connections, and spawning client classes. This class is inherited by two more classes (WorldListener, and MasterListener) that contain more protocol specific overrides and functions.

I also have a helper class (ConsoleWrapper) which encapsulates and extends System.Console, containing methods for writing to console info on what is happening to instances of the WorldListener and MasterListener.

I need a way to determine in the abstract ListenerClass which Inheriting class is calling its methods.

Any help with this problem would be greatly appreciated! I am stumped :X

Simplified example of what I am trying to do.

abstract class Listener
{
   public void DoSomething()
   {
      if(inheriting class == WorldListener)
      ConsoleWrapper.WorldWrite("Did something!");
      if(inheriting class == MasterListener)
      ConsoleWrapper.MasterWrite("Did something!");
   }
}

public static ConsoleWrapper
{
   public void WorldWrite(string input)
   {
       System.Console.WriteLine("[World] {0}", input);
   }
}

public class WorldListener : Listener
{
   public void DoSomethingSpecific()
   {
       ConsoleWrapper.WorldWrite("I did something specific!");
   }
}

public void Main()
{
   new WorldListener();
   new MasterListener();
}

Expected output

[World] Did something!
[World] I did something specific!
[Master] Did something!
[World] I did something specific!

© Stack Overflow or respective owner

Related posts about c#

Related posts about abstract-class