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!
If you know each of the types you want to compare against, then use the
isoperator:Alternatively, you could use
GetTypeif you want a little more flexibility:You should be careful with this approach, however; it’s generally indicative of bad design that you need to explicitly check for types (as these lovely people insist). Instead, it’s almost always more appropriate to use polymorphism to delegate the desired behaviour to the base class (using a virtual or abstract method in the base class) – this is, after all, what it’s designed for!
You might apply polymorphism something like this:
This will produce the output
[World] Did something!. The advantage of this approach is that if you ever want to add another type of listener, it’s simply a matter of defining a new class for it with the appropriateDecoratemethod; there’s no need to modifyListeneritself.