CLR Via C # reading notes---assembly loading and reflection

Source: Internet
Author: User

#1 Loading Assemblies

Assembly.Load:

public class assembly{public    static Assembly Load (AssemblyName assemblyref);    public static Assembly Load (String assemblystring);}

Internally, load causes the CLR to apply a version-binding redirection policy to the application set and to find the assembly in the GAC. If you pass a weakly named assembly, the version binding redirection policy is not applied and the assembly is not located in the GAC.

Appdomain.load:

public class appdomain{Public    Assembly Load (String assemblystring);

Appdomain.load is an instance method, in order to load an assembly, the CLR will use the settings associated with the specified AppDomain instead of the one associated with the AppDomain making the call. Generally avoid use.

Assembly.LoadFrom:

public class assembly{public    static Assembly LoadFrom (String path);

Internally, Assemblyname.getassemblyname (String AssemblyName)-->assembly.load (AssemblyName assemblyref); The CLR then applies the version binding redirection policy. LoadFrom allows you to pass a URL as an argument, for example:

Assembly a = Assembly.LoadFrom (@ "Http://wintellect.com/SomeAssembly.dll");

Assembly.loadfile:

public class assembly{public    static Assembly LoadFile (String path);

LoadFile can load an assembly from any path, and the CLR does not automatically resolve any dependency issues. You need to enlist through the Assemblyresolve event to have the callback method display the loaded dependent assembly.

Assembly.reflectiononlyloadfrom & Assembly.reflectiononlyload:

public class assembly{public    static Assembly Reflectiononlyloadfrom (String assemblyfile);    public static Assembly ReflectionOnlyLoad (String assemblystring);

Reflectiononlyloadfrom load path Specifies a file, does not get a strong name, and does not search the GAC and other locations for files;

ReflectionOnlyLoad will search for the assembly in the GAC, application base directory, private path, and codebase specified location, but no versioning policy will be applied. Which version to load, and which version to get. The CLR prohibits execution of any code in an assembly that is loaded in either of the upper two ways.

The practice of deploying only one EXE file: For the added DLL, change the build action in its properties to embedded resource. At run time, the CLR cannot find a dependent DLL assembly, which needs to be handled as follows.

Application of #2 Reflection

Discover the types defined in the assembly:

The exact meaning of the type object:

C # operator (typeof), test exact match, is/as operator, tested for compatible matches (can match derived objects).

To build an instance of the exception derived class:

#3 designing an application that supports add-ons

#4 members using reflection discovery types

#5 Discover types of interfaces

CLR Via C # reading notes---assembly loading and reflection

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.