The differences between the Assembly. Load () method, Assembly. LoadFrom () method, and Assembly. LoadFile () method!

Source: Internet
Author: User
1, Assembly. Load ()
This method loads an assembly by using the Assembly's long name (including the Assembly name, version information, language culture, and public key tag) and loads other Assembly referenced by the Assembly, generally, this method should be used first. Its execution efficiency is much higher than LoadFrom, and it will not cause repeated loading problems (the reason is described at 2nd points)
When using this method, CLR will apply certain policies to find the Assembly. In fact, CLR locates the assembly in the following order:
(1) If the Assembly has a strong name, first find the assembly in the global assembly easing (GAC.
(2) If the strong name of the Assembly is not correctly specified or cannot be found in GAC, you can find it through the URL specified by the <codebase> element in the configuration file.
(3) If a strong name is not specified or cannot be found in GAC, CLR detects a specific folder:
Assume that your application directory is C: \ AppDir. In the <probing> element, privatePath specifies a path Path1. The Assembly you want to locate is AssemblyName. dll, CLR locates the assembly in the following order
C: \ AppDir \ AssemblyName. dll
C: \ AppDir \ AssemblyName. dll
C: \ AppDir \ Path1 \ AssemblyName. dll
C: \ AppDir \ Path1 \ AssemblyName. dll
If the above method cannot find the Assembly, a compilation error will occur. If the Assembly is dynamically loaded, an exception will be thrown at runtime!
2, Assembly. LoadFrom ()
This method loads the Assembly from the specified path. When this method is called, the CLR will open this file to obtain information such as the assembly version, language culture, and public key tag, pass them to the Load method. Then, the Load method uses the above policy to find the assembly. If an assembly is found, it is compared with the path specified in the LoadFrom method. If the path is the same, the Assembly is considered to be part of the application. If the path is different or the Load method does not find the assembly, the Assembly is loaded as a "data file" and will not be considered as part of the application. This is why the Load method mentioned at is more efficient than the LoadFrom method. In addition, because the Assembly may be loaded as a "data file", when LoadFrom is used to load the same assembly from different paths, it will cause repeated loading. Of course, this method will load other Assembly referenced by this Assembly.
3, Assembly. LoadFile ()
This method loads the Assembly from the specified file. The difference with the above method is that this method does not load other Assembly referenced by this Assembly!
Conclusion: Generally, the Load method should be preferred to Load the Assembly. If you need to use the LoadFrom method, you 'd better change the design and use the Load method instead!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.