Difference between LoadFile and LoadFrom with .NET

2019-01-02 23:22发布

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?

8条回答
贪生不怕死
2楼-- · 2019-01-02 23:58

After a lot of head-scratching I've discovered a difference myself this afternoon.

I wanted to load a DLL at runtime, and the DLL lived in another directory. That DLL had its own dependencies (DLLs) which also lived in that same directory.

LoadFile(): Loaded the specific DLL, but not the dependencies. So when the first call was made from within the DLL to one of those other DLLs it threw a FileNotFoundException.

LoadFrom(): Loaded the DLL that I specified and also all the dependencies that lived in that directory.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-02 23:58

.NET has different load context. Suzanne Cook wrote about them here: http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx

This is the way .Net quarantines that references are not mixed up.

查看更多
登录 后发表回答