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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:26:07+00:00 2026-05-28T07:26:07+00:00

Goal I have a generic class GenericClass<T> and I want to pool instances. I’m

  • 0

Goal

I have a generic class GenericClass<T> and I want to pool instances.
I’m interested in seeing if I can get the syntax:

MyGenericPool = new GenericPool<GenericClass>();
// Or maybe it's MyGenericPool = new GenericPool<GenericClass<>>();

GenericClass<TGenericParam> GenericClassInstance =
    MyGenericPool.Get<TGenericParam>();

(My understanding of generics says, no I can’t, don’t be silly the syntax doesn’t exist / wouldn’t work, but I’m intested in what others think).


Showing my workings

I’m a bit doubtful as from my understanding the types GenericClass<string> and GenericClass<int> aren’t really related from the type system’s point of view.

Now, I realise that I can get close, i.e.:

GenericClass<TGenericParam> GenericClassInstance =
    GenericPool.Get<GenericClass<TGenericParam>>();

and then have the GenericPool just store a Dictionary<Type, ObjectPool<object>> somewhere.
I’m interested in seeing if I can avoid having to do that. I don’t want to have to specify the generic type every time when, as the caller, i’m only changing the generic type parameter. I’d also like to be able to enforce (compile time) that all objects going into my GenericObjectPool<T> are of a set generic type (T<>).


I think the problem stems from not being able to treat a generic type parameter as being generic its self. If I could do that (can I already??) then maybe something like the below might work:

public class GenericClassPool<TGeneric> where TGeneric : class<>
{
    private readonly Dictionary<Type, object> objectPools = new Dictionary<Type, object>();


    private void EnsureObjectPoolExists<TGenericParam>()
    {
        if (!objectPools.ContainsKey(typeof(TGenericParam)))
        {
            objectPools.Add(typeof(TGenericParam), new ObjectPool<TGeneric<TGenericParam>>(() => Activator.CreateInstance(typeof(TGeneric<TGenericParam>)) as TGeneric<TGenericParam>));
        }
    }

    private ObjectPool<TGeneric<TGenericParam>> GetPool<TGenericParam>()
    {
        EnsureObjectPoolExists<TGenericParam>();
        return (objectPools[typeof(TGenericParam)] as ObjectPool<TGeneric<TGenericParam>>);
    }

    public void Add<TTypeParam>(TGeneric<TGenericParam> obj)
    {
        EnsureObjectPoolExists<TTypeParam>();

        GetPool<TGenericParam>().Add(obj);
    }

    public TGeneric<TGenericParam> Get<TGenericParam>()
    {
        return GetPool<TGenericParam>().Get() as TGeneric<TGenericParam>;
    }
}

Question

Can I get the syntax I want (at the top)? If not, how close can I get?

  • 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-28T07:26:07+00:00Added an answer on May 28, 2026 at 7:26 am

    The solution / syntax you are trying to achieve doesn’t work that way, because you can’t use a generic type without its type parameters as the type parameter to another generic type.

    However, you could achieve similar results with the following approach:

    1. Create a base class for the class pool that requires you to supply the complete generic type
    2. Create a derived class for the specific generic type

    Something like that:

    public class ObjectPool
    {
        Dictionary<Type, object> _objectPool = new Dictionary<Type, object>();
    
        public void Add<TKey, TValue>(TValue value)
        {
            _objectPool.Add(typeof(TKey), value);
        }
    
        public TValue Get<TKey, TValue>() where TValue : class
        {
            object value;
            if(_objectPool.TryGetValue(typeof(TKey), out value))
                return value as TValue;
            return null;
        }
    }
    
    public class GenericClassPool : ObjectPool
    {
        public void Add<TGenericParam>(GenericClass<TGenericParam> obj)
        {
            Add<TGenericParam, GenericClass<TGenericParam>>(obj);
        }
    
        public GenericClass<TGenericParam> Get<TGenericParam>()
        {
            return Get<TGenericParam, GenericClass<TGenericParam>>();
        }
    }
    

    Usage would then be like this:

    var pool = new GenericClassPool();
    pool.Add(new GenericClass<string> { Property = "String" });
    pool.Add(new GenericClass<int> { Property  = 0 });
    
    GenericClass<string> firstObject = pool.Get<string>();
    GenericClass<int> secondObject = pool.Get<int>();
    

    The draw back of this solution is that you would need to create one pool class for each generic type you want to pool, so you potentially will have a lot of <className>Pool classes deriving from ObjectPool.
    To make this usable, all real code needs to be in the ObjectPool class and only code that supplies the generic parameters remains in the derived classes.

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

Sidebar

Related Questions

Final goal: Have a few java objects sharing the same base class persisted into
goal: I have the string 1234432144 I want to only replace the first 2
I have a peculiar problem here. I want to extract some generic types, which
I have a Model class for a Generic Object that contains an array of
I have an abstract generic view-model that I use as a base-class for several
I have a generic class which could use a generic OrderBy argument the class
Let me first describe my goal: I have created an object with 3 properties:
The goal is to have the member variable _AddValue point to the CreateFirstValue function
My goal is to have an Android phone (Samsung Galaxy Nexus) communicate with a
My goal is to have the url routing as following: http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page What is

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.