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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:04+00:00 2026-05-27T14:10:04+00:00

Short version: How would i go about creating an object pool that can store

  • 0

Short version:

How would i go about creating an object pool that can store classes of different types all derived from the same base class?
See below for an example of expected usage.


Long version:

I have a class BaseComponent, with many derived classes e.g. Child1Component, Child2Component.

I also have another object that represents a collection of these components, with their properties set to specific values. I call this an EntityTemplate, since an entity is defined by the set of components and their values.

I want to create entities based on entity components. To do this currently i get the appropriate EntityTemplate, loop through it’s different components and call a Clone method I’ve defined on each child class. I also have a Copy method defined there too, which might be useful.

When an entity expires i’d like to add its components to an object pool, then when i next need to create an entity i’d get the entity template and for each component i’d get one of the same type out of the pool and set it’s properties equal to the one in the EntityTemplate, something like below:

// What i want to do
var entityTemplate = GetTemplate("UniqueString");
var MyActualEntity = new Entity();

foreach(var componentTemplate in entityTemplate)
{
    var actualComponent = MagicComponentPool
                              .GetComponentSameTypeAsParam(componentTemplate);
    actualComponent.CopyFrom(componentTemplate);

    MyActualEntity.Components.Add(actualComponent);
}
  • 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-27T14:10:04+00:00Added an answer on May 27, 2026 at 2:10 pm

    I would use a dictionary.

    Dictionary<Type, BaseComponent> dictionary = new Dictionary<Type, BaseComponent>();
    

    put the original components in like this:

    dictionary.Add(component.GetType(), component);
    

    and retrieve them by type.

    BaseComponent component = dictionary[componentTemplate.GetType()];
    

    Complexity of retrieving objects from dictionaries is constant no matter how many objects there’s in the dictionary and is equal to the cost of calculating the hash of the key.

    However, I’m not sure if that is applicable for your purpose, but as you are copying the objects anyway, why not just clone the components from the template or even clone the whole template.

    Here’s a generic Clone method for you:

    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
            public static T Clone<T>(T o)
            {
                byte[] bytes = SerializeBinary(o);
                return DeserializeBinary<T>(bytes);
            }
    
            public static byte[] SerializeBinary(object o)
            {
                if (o == null) return null;
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    bf.Serialize(ms, o);
                    return ms.GetBuffer();
                }
            }
    
            public static T DeserializeBinary<T>(byte[] bytes)
            {
                if (bytes == null) return default(T);
                BinaryFormatter bf = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    return (T) bf.Deserialize(ms);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: How can I map two columns from table A and B if
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
Short Version I have a list of int s that I need to figure
Short version: Can we read from dozens or hundreds of table partitions in a
This question is about a general technique in SQL, that I can't quite work
I would like to distribute an application, but have license key that they can
Short version of my questions: Can anyone point me toward some good, detailed sources
We had a meeting this morning about how would should store our ID for
Short Version How can I draw short text labels in an OpenGL mapping application
It doesn't seem that PHP is short on XML-RPC solutions. I would prefer to

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.