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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:26+00:00 2026-05-26T04:49:26+00:00

Here’s a link to the previous question I had, which led me to this

  • 0

Here’s a link to the previous question I had, which led me to this one.
C# Nested foreach loop optimization

There is still high computation time, and I’m not sure what the cause is.

object foo;
List<string> StringList = new List<string>(); // Populated by previous code
Dictionary<string, Type> assemblyTypes = RandomAssembly.GetTypes().ToDictionary(t => t.Name, t => t);

  foreach (String name in StringList)
  {                     
    if (assemblyTypes.ContainsKey(name))
    {
      // Create an instance of the matching class and add it
      // to the appropriate lists.
      Type at = assemblyTypes[name];
      foo = Activator.CreateInstance(at);       
      ArbitraryList1.Add(foo);
    }
  }    
  • 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-26T04:49:27+00:00Added an answer on May 26, 2026 at 4:49 am

    If the computation time is noticeably slow I guess you’re calling into this code a lot and you’re newing up a lot of objects that you don’t know much about at compile time.

    Instead of keeping the classes in a Dictionary<string, type> and call CreateInstance you want to keep a dictionary of their constructors Dictionary<string, ConstructorInfo> so you can call them directly without having to look for it every time with reflection.

    That way you can just call AssemblyConstructors[Name].Invoke() to create a new instance of the class.

    This way you only have to use reflection once to find the constructors.

    // keep a dictionary of constructors (instead of the types)
    var Constructors = new Dictionary<string, ConstructorInfo>();            
    
    // add this class to this dictionary
    Type t = typeof(SomeClass);            
    string name = t.Name;
    Constructors[name] = t.GetConstructors()[0]; // use reflection only once here, afterwards we reuse the reflected info
    
    // new up an instance 
    var o = Constructors[name].Invoke(new object[] {}); 
    

    I think the first constructor will be the parameterless one. Else try something like t.GetConstructors().Where(x => x.GetParameters().Count() == 0).First();
    This is the fastest easy way I know because apparently you can’t get a delegate to a constructor.

    If you write the classes you’re newing up yourself, you can have a common base class or interface with a method that creates one, that way you can keep a delegate to that constructor, that’s even faster.

    This post also has some interesting ideas about this that take optimization much further. If you want to do this fast, you can. Almost as fast as just calling new KnownClass()

    Good luck,

    GJ

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

Sidebar

Related Questions

Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here a simple question : What do you think of code which use try
Here is my question. I am having this simple menu. <div id=menu> <ul> <li>
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's the link: www.mchenry.edu/maps/google.asp Why won't the location balloon display correctly? Also, the pop-up
Here's my situation. I have a DotNetNuke application. I want to link to an
Here is what I am trying to achieve in PHP: I have this string:

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.