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

  • SEARCH
  • Home
  • 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 9259797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:44:37+00:00 2026-06-18T12:44:37+00:00

I do have a class, which is defined as: public abstract class Singleton <T>

  • 0

I do have a class, which is defined as:

public abstract class Singleton <T> : BaseObject
    where T : Singleton <T>
{
}

I want to define an array of those generic singletons somewhere else. Something like

public MonoSingleton[] singletons;

How can I retrieve the proper type of that generic (that seems to be recursive, as you may see)? How can I write this out?

  • 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-06-18T12:44:38+00:00Added an answer on June 18, 2026 at 12:44 pm

    Are you trying to do the ‘curiously recursive template pattern’, like this?

    class CuriouslyRecursiveBase<T>
    {
    
    }
    
    class CuriouslyRecursiveDervied<T> : CuriouslyRecursiveBase<T>
    {
    
    }
    
    class MyClass : CuriouslyRecursiveBase<MyClass>
    {
    
    }
    

    To instantiate the derived from the base, you just use:

    class CuriouslyRecursiveBase<T>
    {
        public static void InstantiateDerived()
        {
            T instance = (T)Activator.CreateInstance(typeof(T));
        }
    }
    

    Since T is actually the derived type (MyClass) and curiously is also type (CuriouslyRecursive<MyClass>).

    Specifically applied to your problem:

    // Create a common interface that all singletons use. This allows 
    // us to add them all to a list.
    interface ISingleton { }
    
    class Singleton<T> : ISingleton
    {
        // Store our list of ISingletons
        static List<ISingleton> instances = new List<ISingleton>();
        static T instance;
    
        protected Singleton() { }
    
        public static T GetInstance()
        {
            // Either return the existing instnace, or create a new one
            if (Singleton<T>.instance == null)
            {
                Singleton<T>.instance = (T)Activator.CreateInstance(typeof(T));
    
                // Use a common interface so they can all be stored together.
                // Avoids the previously mentioned co-variance problem.
                // Also, compiler isn't built to follow curious recursiveness,
                // so use a dynamic statement to force runtime re-evaluation of 
                // the type hierarchy. Try to avoid dynamic statements in general
                // but in this case its useful.
                instances.Add((dynamic)Singleton<T>.instance);
            }
    
            return Singleton<T>.instance;
        }
    }
    
    class MyClass : Singleton<MyClass>
    {
    
    }
    
    public static void Main()
    {
        MyClass my = MyClass.GetInstance();
    }
    

    More info:

    http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

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

Sidebar

Related Questions

I have a code first application which defined like this: public abstract class Entity
I have one supertype defined as: public abstract class AType<T> { .... private T
I have an abstract class called 'Template' defined as: [DataContract] public abstract class Template
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
I have defined an Action pure abstract class like this: class Action { public:
I have an abstract base class from which many classes are derived. I want
I have a base class defined as follow: @MappedSuperclass public abstract class BaseUser {
In my application I have a class which has properties of user-defined types like
i have a class User which extends Ebean Model. And i defined a dbfile
I have a class called MainGame, which is defined like this in my .h:

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.