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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:15:05+00:00 2026-05-29T15:15:05+00:00

I am trying to create an object pool which will have objects of diffrent

  • 0

I am trying to create an object pool which will have objects of diffrent types.

Will it be possible , If i passed a string as ps a parameter to RetriveFunction();
it should rturn a new object of string type or feth it from pool?

string will contain name of type.

Eg;

    Object RetriveFromPool(string typename)
    {
          if()//object does not present
           {
               //return new object of typename
           }
          else
           {
               //feth it from pool
           }
     }

will it be possible?

  • 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-29T15:15:06+00:00Added an answer on May 29, 2026 at 3:15 pm

    Yes, this is possible. A Dictionary is a convenient way to store key value pairs with O(1) lookup and Activator is capable of instantiating a type known only at runtime:

    private IDictionary<string, object> _objectPool;
    object RetriveFromPool(string typeName)
    {     
        if(_objectPool.ContainsKey(typeName))
        {
            return _objectPool[typename]; // return from the pool
        }
        return Activator.CreateInstance(Type.GetType(typeName)); // Try to create a new object using the default constructor
     }
    

    As an alternate however (to ensure compile time type checking) you may wish to use generics to achieve this:

    private IDictionary<Type, object> _objectPool;
    public T RetrieveFromPool<T>() where T : new() 
    {
      Type type = typeof(T);
      return _objectPool.ContainsKey(type) ? (T)_objectPool[type] : new T();
    }
    
    // Update - add a couple of templates for add methods:
    
    public void AddToPool<T>() where T : new
    {
      _objectPool[typeof(T)] = new T();
    }
    
    public void AddToPool<T>(T poolObject) where T : new
    {
      _objectPool[typeof(T)] = poolObject;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an object in python which is composed of smaller objects.
I'm trying to create an object file which can be useable in any computer.
Basically, I'm trying to create an object of unique objects, a set. I had
I have a problem with my WPF program. I'm trying to create an object
I have a VB6 dll that is trying to create a COM object using
In my example, I'm trying to create a new object which has references to
I'm trying to create objects from a key/value list. My Problem is, that Object
I have the dreaded permission denied issue when trying to create an object using
I'm trying to create a class in java that pool objects. The class starts
I'm trying to create an object with a run method that will be wrapped

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.