65.9K
CodeProject is changing. Read more.
Home

JIT Compilation Affects Code

starIconstarIconstarIconstarIconstarIcon

5.00/5 (13 votes)

Jul 22, 2013

CPOL

1 min read

viewsIcon

12685

An interesting code illustrating the influence of Just-In-Time (JIT) compilation in .NET on code formation.

Recently I came across an interesting code illustrating the influence of Just-In-Time (JIT) compilation in .NET on code formation.

Consider the following scenario. A console application refers to some ClassLibrary.dll containing type Class1. The console application in its method Main() calls method ClassLibrary.Class1.Echo(). But we want that ClassLibrary.dll be available to the console application only if the latter has the appropriate license. The handler of the AppDomain.CurrentDomain.AssemblyResolve event checks this and in case the license is proper returns the required assembly ClassLibrary. Thus, the handler of the AppDomain.CurrentDomain.AssemblyResolve event should be called before ClassLibrary.Class1 instantiation. To test this, we need to build both ClassLibrary.dll and the console application, then delete ClassLibrary.dll from the target directory, run the console application, and check whether the AppDomain.CurrentDomain.AssemblyResolve event handler (which is the CurrentDomain_AssemblyResolve() method) is called.

The code below is the console application.

#define ASSEMBLY_RESOLVED_CALLED

using System;
using System.Reflection;
using ClassLibrary;  // contains Class1

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += 
              new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            string s = "...";

#if ASSEMBLY_RESOLVED_CALLED
            Echo(s);
#else
            new Class1().Echo(s);
#endif
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            Assembly assm = null;
            
            //
            // This method supposes to return appropriate assembly.
            // Current implementation is just a dummy.
            //
            
            Console.WriteLine("CurrentDomain_AssemblyResolve is called.");
            Console.ReadKey();
            
            return assm;
        }

#if ASSEMBLY_RESOLVED_CALLED
        private static void Echo(string s)
        {
            new Class1().Echo(s);
        }
#endif
    }
}

If ClassLibrary.dll was deleted from the target directory then to ensure the call of the CurrentDomain_AssemblyResolve() method, ASSEMBLY_RESOLVED_CALLED has to be defined. The reason is as follows. At runtime, the JIT compiler processes every method separately. By the time of this processing all the required components should be already available. But when ASSEMBLY_RESOLVED_CALLED is not defined the method Main() can not be processed by the JIT compiler since ClassLibrary.Class1 is not available. On the contrary, when ASSEMBLY_RESOLVED_CALLED is defined the method Main() can be processed since it does not contain a direct call to ClassLibrary.Class1, and the CurrentDomain_AssemblyResolve() method is called.