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 862635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:10:30+00:00 2026-05-15T09:10:30+00:00

We have the following class hierarchy: public interface IManager { object GetObject(int); } public

  • 0

We have the following class hierarchy:

public interface IManager {
   object GetObject(int);
}

public class BaseManager : IManager ...

public class XManager : BaseManager {
   ...
   public static XManager Instance;
}

public class YManager : BaseManager {
   ...
   public static YManager Instance;
}

public static class ManagerFacade {
   private static IManager GetManager(type);
   public static object GetObject(type, int) { 
     return GetManager(type).GetObject(int); }
}

How would you implement the GetManager() function?

Is it possible to collect these types and their instances (or instance creating delegates) in a static dictionary in the static constructors of these classes?
(Thank you Jon, I only remembered “something is different in .Net 4”, but not the details)

Other ways would use class attributes or descendents of BaseManager or looking for implementations of IManager.

What is the preferred solution?

  • 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-15T09:10:31+00:00Added an answer on May 15, 2026 at 9:10 am

    The fact that static members are involved makes me think that a dictionary-based approach is the best, unless you want to invoke reflection and figure out the method for retrieving the singleton. This is a working sample code I was able to come up with:

    // Added another interface - GetInstance
    public interface IManager
    {
        object GetObject(int i);
    }
    
    // made BaseManager abstract; you don't have to do that, but then
    // remember to make everything virtual, etc etc.
    public abstract class BaseManager : IManager
    {
        public abstract object GetObject(int i);
    }
    

    The child classes each implement a static constructor to create the singleton instance in my example, which is way too simplistic, but I’m sure you have a better way to do this:

    public class XManager : BaseManager
    {
        public static XManager Instance;
    
        static XManager() { Instance = new XManager(); }
    
        public override object GetObject(int i)
        {
            return "XManager Instance: index was " + i.ToString();
        }
    }
    
    public class YManager : BaseManager
    {
        public static YManager Instance;
        static XManager() { Instance = new YManager(); }
    
        public override object GetObject(int i)
        {
            return "YManager Instance: index was " + i.ToString();
        }
    }
    

    The ManagerFacade would implement the dictionary this way:

    public static class ManagerFacade
    {
        private static readonly Dictionary<Type, IManager> managerInstances 
          = new Dictionary<Type, IManager>()
        {
            {typeof(XManager), XManager.Instance},
            {typeof(YManager), YManager.Instance}
        };
    
        private static IManager GetManager<T>() where T: IManager
        {
            return managerInstances[typeof(T)];
        }
    
        public static object GetObject<T>(int i) where T: IManager
        {
            return GetManager<T>().GetObject(i);
        }
    }
    

    The console app to test out the manager facade:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ManagerFacade.GetObject<XManager>(2).ToString());
            Console.WriteLine(ManagerFacade.GetObject<YManager>(4).ToString());
    
            // pause program execution to review results...
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }
    

    Console output:

    XManager Instance: index was 2
    YManager Instance: index was 4
    Press enter to exit
    

    I’m sure that there’s more elegant ways to do this, but I just wanted to illustrate how to set up and access the dictionary to support the singletons.

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

Sidebar

Related Questions

Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class objects: public class VacancyCategory { public int ID {
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable<Row>, IStringIndexable,
I have the following class hierarchy public abstract BaseClass : System.Web.UI.UserControl { public virtual
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
I have the following class public class Car { public Name {get; set;} }
I have the following class [XmlRoot(ElementName= webSites)] //No capital w at the beginning public
I have following classes: public abstract class CustomerBase { public long CustomerNumber { get;

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.