. Net (C #): Only reflectiononlyloadfrom can save strongly-named Assembly conflicting with GAC.

Source: Internet
Author: User

Conclusion: if there are two strongly-named assemblies with the same Assembly name, one is in GAC, and the other is not. How to dynamically load the Assembly not in the GAC? The answer is that only assembly. reflectiononlyloadfrom can be loaded.

 

For example, we copy the system. dll file to the D disk (note that mscorlib. dll is loaded by default in the application domain, instead of system. dll ). Then try the following method:

VaR Path = @ "D: \ system. dll ";

 

// Load (byte [])

Assembly. Load (file. readallbytes (PATH ));

// Loadfrom (string)

Assembly. loadfrom (PATH );

// LoadFile (string)

Assembly. LoadFile (PATH );

 

Next, enumerate the loaded assembly path:

Foreach (VAR ass in appdomain. currentdomain. getassemblies ())

Console. writeline (ass. Location );

 

The output will contain system. dll in. NET Framework GAC, instead of the system. dll in disk D.

C: \ windows \ microsoft. Net \ Assembly \ gac_msil \ System \ v4.0 _ 4.0.0.0 _ b77a5c561934e089 \ system. dll

 

Okay, this file can be used only after reflectiononlyloadfrom.

Code:

VaR Path = @ "D: \ system. dll ";

 

// Reflectiononlyloadfrom (string)

Assembly. reflectiononlyloadfrom (PATH );

 

// Use appdomain. reflectiononlygetassemblies ()

Foreach (VAR ass in appdomain. currentdomain. reflectiononlygetassemblies ())

Console. writeline (ass. Location );

 

Output:

D: \ system. dll

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.