Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.Load performance impact

I'm working on a component which executes provisional method of any interface registered with ioc, and execution moment depends on different triggers. It must be able to save actions to be performed to database, so I'm saving method name, type and list of parameters (serialized to BLOB) into database until needed.

When trigger occurs, I need to execute method on a instance of type. As I'm using dependency injection, I have interface name saved into database (in format "Namespace.IInterface, AssemblyName")

To run Resolve<IInterface>() method on ioc container, I need instance of its Type:

Assembly assembly = System.Reflection.Assembly.Load(assemblyName);
Type service = assembly.GetType(typeName);
object instance = IOCContainer.Resolve(service);

My questions are:

  • Is there a better way to get an instance of Type from its name, if I'm sure that containing assembly is already loaded into app domain? (I tried with simply Type.Load(typeName) but got null)
  • If assembly in question is already loaded, will CLR optimize that process (use already loaded), or I need to manually cache list of assemblies to prevent performance impact of repeatedly loading same assembly over and over?
like image 727
Goran Obradovic Avatar asked Dec 20 '22 16:12

Goran Obradovic


1 Answers

  • If typeName that you use includes assembly name(something like MyNamespace.MyType, MyAssembly version=1.0.0.0,publicKeyToken=12345etc) then Type.Load(typeName) will get your type but not null;
  • CLR takes care of loading assembly only once(once per context, details is here, in your case context remains the same, so the answer is that you should relax and leave the caching up to the CLR :)).
like image 154
alex.b Avatar answered Dec 30 '22 10:12

alex.b