Click here to Skip to main content
Click here to Skip to main content

Tagged as

JIT Compilation Affects Code

, 22 Jul 2013
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.

#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)

Share

About the Author

Igor Ladnik
Architect
Israel Israel


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

Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pinmembertimuri23-Jul-13 4:48 
GeneralRe: My vote of 5 PinprofessionalIgor Ladnik23-Jul-13 15:06 
GeneralMy vote of 5 Pinmemberpatel_pratik23-Jul-13 1:36 
GeneralRe: My vote of 5 PinprofessionalIgor Ladnik23-Jul-13 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Jul 2013
Article Copyright 2013 by Igor Ladnik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid