Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 728907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:44:07+00:00 2026-05-14T06:44:07+00:00

In my console application have an abstract Factory class Listener which contains code for

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:44:07+00:00Added an answer on May 14, 2026 at 6:44 am

    If you know each of the types you want to compare against, then use the is operator:

    if (this is WorldListener)
    {
     // ...
    }
    else if (this is MasterListener)
    {
     // ...
    }
    

    Alternatively, you could use GetType if you want a little more flexibility:

    var type = GetType();
    // Do some logic on the type to determine what to do next.
    

    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:

    static class Program
    {
        static void Main(string[] args)
        {
            Listener listener = new WorldListener();
            listener.DoSomething();
        }
    }
    
    abstract class Listener
    {
        public void DoSomething()
        {
            var output = Decorate("Did something!");
            ConsoleWrapper.WriteLine(output);
        }
    
        protected abstract string Decorate(string input);
    }
    
    class WorldListener : Listener
    {
        protected override string Decorate(string input)
        {
            return string.Format("[World] {0}", input);
        }
    }
    
    class MasterListener : Listener
    {
        protected override string Decorate(string input)
        {
            return string.Format("[Master] {0}", input);
        }
    }
    

    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 appropriate Decorate method; there’s no need to modify Listener itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application that require to use some code that need administrator
I have a console application. A class (let's say Worker) does some work in
I have a console application that contains quite a lot of threads. There are
I have a console application from which I create a window. I can render
I have a console application which is intended to just keep running until it
I have a c# console application which has some threads to do some work
I have a console application that will be kicked off with a scheduler. If
I have a Console application hosting a WCF service. I would like to be
I have a trivial console application in .NET. It's just a test part of
I have a console application project in C# 2.0 that needs to write something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.