How does CLR load the Assembly and the Assembly Version Policy, and clr load the version policy?

Source: Internet
Author: User

How does CLR load the Assembly and the Assembly Version Policy, and clr load the version policy?

Reference page:

Http://www.yuanjiaocheng.net/CSharp/Csharp-for-loop.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-while-loop.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-do-while-loop.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-structure.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-enum.html

In the project configuration file Web. in config, you will see the <runtime> node and the <assemblyBinding> node contained in it. This is obviously related to the Assembly. When will these nodes be used?

 

By default, during runtime, the JIT compiler will check the types referenced by fields, local variables, and method parameters in the IL code when compiling the code at the local cost, then, the System is used internally with the help of the TypeRef and AssemblyRef metadata of the Assembly. reflection. the Load method of Assembly to determine the Assembly to be loaded, including the module.

 

The Load method receives a string that represents a strongly-typed assembly, similar to the following:

 

Assembly a = Assembly.Load(
    "somename, Version=1.2.3.4" +
    "Culture=neutral, PublicKeyToken=........"
);
 
a.CreateInstance("someclassname");


Inside the Load method, an assembly resolver mechanism is used to find the assembly. First, we will follow the "Version Policy" to find the appropriate version of the Assembly. This "Version Policy" can be configured in the configuration file. In "C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 \ Config \ machine. config "to complete the computer-level configuration in the current project's Web. complete the application-level configuration in config. For example, in Web. config:

 

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

 

If you want to cancel "Version Policy", you can configure it as follows in Web. config:

 

<runtime>
    <rt:assemblyBinding>
      <rt:publisherPolicy apply="no" />
    </rt:assemblyBinding>
</runtime>


 

Above, the Assembly is loaded on demand. When the program runs, you need to use a certain type to load the assembly of this type and load this type into the memory.

 

If you do not want the assembly to be loaded as needed, either set the type to static or directly tell the CLR how to load the assembly. You can use the static method LoadFrom of System. Reflection. Assembly to load a fixed Assembly.


For example, create a class library named Customer and create a CustomerBehavior class.

 

using System;
 
namespace Customer
{
    public class CustomerBehavior
    {
        public void SayHello()
        {
            Console.WriteLine("hello");
        }
    }
}

 

Set the path for generating the Customer class library to the temp folder of drive F. The client dynamically loads the Customer. dll assembly under the temp folder of the F disk through LoadFrom.

 

        static void Main(string[] args)
        {
            Assembly a = Assembly.LoadFrom("f:\\temp\\Customer.dll");
            var type = a.GetType("Customer.CustomerBehavior");
            Console.WriteLine(type.Assembly.FullName);
            Console.ReadKey();
        }


 

Summary: The CLR uses the Load method of System. Reflection. Assembly to Load the Assembly. The loaded Assembly version policy can be set through the configuration file.

 

 


 

 

Related Article

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.