Click here to Skip to main content
11,926,961 members (46,809 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked

JIT Compilation Affects Code

, 22 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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 = "...";

            new Class1().Echo(s);

        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.");
            return assm;

        private static void Echo(string s)
            new Class1().Echo(s);

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.


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


About the Author

Igor Ladnik
Israel Israel

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
timuri23-Jul-13 5:48
membertimuri23-Jul-13 5:48 
GeneralRe: My vote of 5 Pin
Igor Ladnik23-Jul-13 16:06
professionalIgor Ladnik23-Jul-13 16:06 
GeneralMy vote of 5 Pin
patel_pratik23-Jul-13 2:36
memberpatel_pratik23-Jul-13 2:36 
GeneralRe: My vote of 5 Pin
Igor Ladnik23-Jul-13 2:58
professionalIgor Ladnik23-Jul-13 2: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 22 Jul 2013
Article Copyright 2013 by Igor Ladnik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid