Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid loading an assembly dynamically that I have already loaded using Reflection?

I am loading assemblies using Assembly.LoadFile(assemblyFilePath) in a loop and I want to avoid calling Assembly.LoadFile if the assembly has already be loaded once. Should I be concerned about calling Assembly.LoadFile repeatedly for a DLL that has already been loaded? Thanks.

like image 310
Achilles Avatar asked Oct 19 '11 17:10

Achilles


People also ask

Which event allows you to intervene and manually load an assembly that the CLR Cannot find?

AssemblyResolve event, to handle the attempt to load an assembly when the automatic CLR search has failed.

Which of the following methods from the assembly class can be used to load an assembly from a byte array?

To load an assembly from a byte array with the trust level of the application domain, use the Load(Byte[], Byte[], SecurityContextSource) method overload.

How do you unload assembly from Appdomain?

You can not unload an assembly from an appdomain. You can destroy appdomains, but once an assembly is loaded into an appdomain, it's there for the life of the appdomain.

What is the method to load assembly given its file name and its path?

LoadFrom(String) Loads an assembly given its file name or path.


1 Answers

No you don't need to be concerned because if an assembly has already been loaded it won't be loaded again

If you call Assembly.LoadFile() then you can load the same assembly multiple times but only if you are loading assembly from different path each time. You can use Assembly.Load() which will load an assembly only once. You can also find about already loaded assemblies in current app domain using

Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
like image 62
Haris Hasan Avatar answered Sep 22 '22 16:09

Haris Hasan