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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:29:26+00:00 2026-05-19T03:29:26+00:00

I am currently looking at optimising an C# (.NET 3.5) application framework, and at

  • 0

I am currently looking at optimising an C# (.NET 3.5) application framework, and at the moment I am looking at portions of the code that use Activator.CreateInstance to invoke assembly methods via interfaces. As an example I have the following the following, fully functional, code:

private object InvokeAssembly(string assemblyPath, string assemblyType, string data)
{
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    Type type = assembly.GetType(assemblyType, true, true);
    IMyInterface assemblyInterface = (IMyInterface)Activator.CreateInstance(type);

    return assemblyInterface.DoSomething(data);
}

The question is, is that a good design? Especially when considering that this particular method is called 100’s of times a minute, so would the following singleton-esque pattern be a better design?

Improved sample code, courtesy of JaredPar

Dictionary<string, IMyInterface> assemblyCache = new Dictionary<string, IMyInterface>();

private object InvokeAssembly(string assemblyPath, string assemblyType, string data)
{
    var cacheKey = assemblyPath + "*" + assemblyType;

    IMyInterface assemblyInterface;

    if (!this.assemblyCache.TryGetValue(cacheKey, out assemblyInterface))
    {
        Assembly assembly = Assembly.LoadFrom(assemblyPath);
        Type type = assembly.GetType(assemblyType, true, true);
        assemblyInterface = (IMyInterface)Activator.CreateInstance(type);
        assemblyCache[cacheKey] = assemblyInterface;
    }

    return assemblyInterface.DoSomething(data);
}

In this second example the interface is created once and reused rather than created/collected once per request.

It seems logical that the second method is the better solution, however I am more than happy to be told that it makes no difference.

It is also worth mention that this method and only this method operates on the Assembly

  • 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-19T03:29:27+00:00Added an answer on May 19, 2026 at 3:29 am

    The second solution will perform better but has a bug. It cache’s the IMyInterface instance and reuses it across method calls without bothering to check that the same assembly path and type were passed into the method. Consequetive calls with different path or type values will cause the wrong IMyInterface instance to be used. The caching pattern needs to be aware of this.

    One way to do this is to use a Dictionary<string, IMyInterface> to cache the types.

    Dictionary<string, IMyInterface> _map = new Dictionary<string, IMyInterface>();
    
    private object InvokeAssembly(string assemblyPath, string assemblyType, string data)
    {
      var key = assemblyPath + "*" + assemblyType;
      IMyInterface data;
      if (!_map.TryGetValue(key, out data)) {
        Assembly assembly = Assembly.LoadFrom(assemblyPath);
        Type type = assembly.GetType(assemblyType, true, true);
        data = (IMyInterface)Activator.CreateInstance(type);
        _map[key] = data;
      }
      return data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently looking at migrating from fluent nHibernate to ADO.Net Entity Framework 4. I
I am currently looking at an application that uses Spring and Spring JDBC. http://static.springsource.org/spring/docs/2.0.x/reference/jdbc.html
I am currently looking at choosing a automated web testing framework for use with
I am currently looking into a couple of possibilities for a microsite that I
I'm currently looking into automating a flex build so that we can get it
I am currently looking at creating a script for my site that will count
I'm currently looking for a way to optimize a process that takes a long
I'm currently looking how to work with caching on my web application I'm buildning.
I'm currently building a web application using Zend framework 1.11.11 and Doctrine 2.2. I
I'm currently looking for good (and free if possible) Web RAD (Rapid Application Development)

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.