Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between LoadFile and LoadFrom with .NET Assemblies?

I was looking at the msdn documentation and I am still a little confused on what exactly is the difference between using LoadFile and LoadFrom when loading an assembly. Can someone provide an example or an analogy to better describe it. The MSDN documentation confused me more. Also, Is ReflectionOnlyLoadFrom the same as LoadFrom except that it loads the assembly only in reflection mode.

Since my .NET experience is not the greatest, here are some questions regarding the MSDN documentation using LoadFile:

1) What does it mean by LoadFile examines assemblies that have the same Identity, but are located in different paths? What is the identity (example)?

2) It states the LoadFile does not load files into the 'LoadFrom Context' and does not resolve dependencies using the load path. What does this mean, can someone provide an example?

3) Lastly, it states that LoadFile is useful in this limited scenario because LoadFrom cannot load assemblies that have the same identities but different paths; it will only load the first such assembly, which again brings me to the same question, what is the assemblies identity?

like image 816
Xaisoft Avatar asked Oct 10 '22 16:10

Xaisoft


People also ask

When would using assembly LoadFrom or assembly LoadFile be appropriate?

So, you should use LoadFrom when you need to prevent loading second assembly with same identity or just loading assembly twice. Respectively, you should use LoadFile only if you need to load assembly twice or load two assemblies with same identifier.

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

What is System reflection assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

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.


2 Answers

Does this clear it up?

// path1 and path2 point to different copies of the same assembly on disk:

Assembly assembly1 = Assembly.LoadFrom(path1);
Assembly assembly2 = Assembly.LoadFrom(path2);

// These both point to the assembly from path1, so this is true
Console.WriteLine(assembly1.CodeBase == assembly2.CodeBase);

assembly1 = Assembly.LoadFile(path1);
assembly2 = Assembly.LoadFile(path2);

// These point to different assemblies now, so this is false
Console.WriteLine(assembly1.CodeBase == assembly2.CodeBase);

Edit: to answer the questions you raised in your revised question, you definitely want to read Suzanne Cook on Assembly Identity.

There are a lot of rules that govern how assemblies are loaded, and some of them have to do with how they resolve dependencies - if your AssemblyA is dependent on AssemblyB, where should .NET look to find AssemblyB? In the Global Assembly Cache, the same directory it found AssemblyA, or somewhere else entirely? Furthermore, if it finds multiple copies of that assembly, how should it choose which one to use?

LoadFrom has one set of rules, while LoadFile has another set of rules. It is hard to imagine many reasons to use LoadFile, but if you needed to use reflection on different copies of the same assembly, it's there for you.

like image 123
Jeff Sternal Avatar answered Oct 22 '22 03:10

Jeff Sternal


From Suzanne Cook's blog:

LoadFile vs. LoadFrom

Be careful - these aren't the same thing.

LoadFrom() goes through Fusion and can be redirected to another assembly at a different path but with that same identity if one is already loaded in the LoadFrom context.

LoadFile() doesn't bind through Fusion at all - the loader just goes ahead and loads exactly* what the caller requested. It doesn't use either the Load or the LoadFrom context.

So, LoadFrom() usually gives you what you asked for, but not necessarily. LoadFile() is for those who really, really want exactly what is requested. (*However, starting in v2, policy will be applied to both LoadFrom() and LoadFile(), so LoadFile() won't necessarily be exactly what was requested. Also, starting in v2, if an assembly with its identity is in the GAC, the GAC copy will be used instead. Use ReflectionOnlyLoadFrom() to load exactly what you want - but, note that assemblies loaded that way can't be executed.)

LoadFile() has a catch. Since it doesn't use a binding context, its dependencies aren't automatically found in its directory. If they aren't available in the Load context, you would have to subscribe to the AssemblyResolve event in order to bind to them.

See here.

Also see Choosing a Binding Context article on the same blog.

like image 24
CraigTP Avatar answered Oct 22 '22 04:10

CraigTP