Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Tip/Trick

JIT Compilation Affects Code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
22 Jul 2013CPOL1 min read 12.2K   10   4
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.

C#
#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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner



Comments and Discussions

 
GeneralMy vote of 5 Pin
timuri23-Jul-13 4:48
timuri23-Jul-13 4:48 
GeneralRe: My vote of 5 Pin
Igor Ladnik23-Jul-13 15:06
professionalIgor Ladnik23-Jul-13 15:06 
GeneralMy vote of 5 Pin
patel_pratik23-Jul-13 1:36
patel_pratik23-Jul-13 1:36 
GeneralRe: My vote of 5 Pin
Igor Ladnik23-Jul-13 1:58
professionalIgor Ladnik23-Jul-13 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.