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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:03:21+00:00 2026-05-23T23:03:21+00:00

In our game (targeted at mobile) we have a few different entity types and

  • 0

In our game (targeted at mobile) we have a few different entity types and I’m writing a factory/repository to handle instantiation of new entities. Each concrete entity type has its own factory implementation and these factories are managed by an EntityRepository.

I’d like to implement the repository as such:

Repository
{
 private Dictionary <System.Type, IEntityFactory<IEntity>> factoryDict;

 public T CreateEntity<T> (params) where T : IEntity
 {
      return factoryDict[typeof(T)].CreateEntity() as T;
 }
}

usage example

var enemy = repo.CreateEntity<Enemy>();

but I am concerned about performance, specifically related to the typeof(T) operation in the above. It is my understanding that the compiler would not be able to determine T’s type and it will have to be determined at runtime via reflection, is this correct? One alternative is:

Repository
{
 private Dictionary <System.Type, IEntityFactory> factoryDict;

 public IEntity CreateEntity (System.Type type, params)
 {
      return factoryDict[type].CreateEntity();
 }
}

which will be used as

var enemy = (Enemy)repo.CreateEntity(typeof(Enemy), params);

in this case whenever typeof() is called, the type is on hand and can be determined by the compiler (right?) and performance should be better. Will there be a noteable difference? any other considerations? I know I can also just have a method such as CreateEnemy in the repository (we only have a few entity types) which would be faster but I would prefer to keep the repository as entity-unaware as possible.

EDIT:

I know that this may most likely not be a bottleneck, my concern is just that it is such a waste to use up time on reflecting when there is a slightly less sugared alternative available. And I think it’s an interesting question 🙂

I did some benchmarking which proved quite interesting (and which seem to confirm my initial suspicions).

Using the performance measurement tool I found at
http://blogs.msdn.com/b/vancem/archive/2006/09/21/765648.aspx
(which runs a test method several times and displays metrics such as average time etc) I conducted a basic test, testing:

private static T GenFunc<T>() where T : class 
    {

        return dict[typeof(T)] as T;
    }

against

    private static Object ParamFunc(System.Type type)
    {
        var d = dict[type];
        return d;
    }

called as

str = GenFunc<string>();

vs

str = (String)ParamFunc(typeof(String));

respectively. Paramfunc shows a remarkable improvement in performance (executes on average in 60-70% the time it takes GenFunc) but the test is quite rudimentary and I might be missing a few things. Specifically how the casting is performed in the generic function.

An interesting aside is that there is little (neglible) performance gained by ‘caching’ the type in a variable and passing it to ParamFunc vs using typeof() every time.

  • 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-23T23:03:23+00:00Added an answer on May 23, 2026 at 11:03 pm

    Generics in C# don’t use or need reflection.

    Internally types are passed around as RuntimeTypeHandle values. And the typeof operator maps to Type.GetTypeFromHandle (MSDN). Without looking at Rotor or Mono to check, I would expect GetTypeFromHandle to be O(1) and very fast (eg: an array lookup).

    So in the generic (<T>) case you’re essentially passing a RuntimeTypeHandle into your method and calling GetTypeFromHandle in your method. In your non-generic case you’re calling GetTypeFromHandle first and then passing the resultant Type into your method. Performance should be near identical – and massively outweighed by other factors, like any places you’re allocating memory (eg: if you’re using the params keyword).

    But it’s a factory method anyway. Surely it won’t be called more than a couple of times per second? Is it even worth optimising?

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

Sidebar

Related Questions

For our online game, we have written tons of PHP classes and functions grouped
In error reports from some end users of our game I have quite often
We're developing a silverlight multiplayer game using TCP connections. We have all of our
In a multiplayer game I'm developing, we have a few values that are floating
Due to lack of capital and time we are having to do our game
Our company is currently writing a GUI automation testing tool for compact framework applications.
Our team is creating a new recruitment workflow system to replace an old one.
Our application is interfacing with a lot of web services these days. We have
I'm working on an RTS game in C++ targeted at handheld hardware (Pandora). For
Since having blends is hitting perfomance of our game, we tried several blending strategies

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.