Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Loading and unloading assemblies using reflection

Loading and unloading assemblies using reflection

Scheduled Pinned Locked Moved C#
csharpvisual-studiodebuggingquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nitin_ion
    wrote on last edited by
    #1

    string fileToLoad = @"C:\Users\nitin.jain\Documents\visual studio 2012\Projects\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
    string migrationStepsDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClassLibrary1.dll");
    AppDomainSetup appDomainSetup = new AppDomainSetup() { PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory, ShadowCopyFiles="true" };
    Evidence evidence = AppDomain.CurrentDomain.Evidence;

            AppDomain appDomain = AppDomain.CreateDomain("ClassLibrary1", evidence, appDomainSetup);
    
            //WORKING
            Assembly assembly = Assembly.LoadFile(fileToLoad);
    
            //This part works well
            Type type = assembly.GetType("ClassLibrary1.Class1");
            object foo = Activator.CreateInstance(type);
            MethodInfo methodInfo = type.GetMethod("CheckAdress");
            methodInfo.Invoke(foo, new object\[\] { "navdeep" });
            AppDomain.Unload(appDomain);
            Console.WriteLine("App domain unloaded");
            Console.ReadLine();
    

    This code is in console application and it works fine in the sense that it loads and unload the assembly in the domain. But when my console is running and i try to delete the dll it says that dll is opened in console application. So it is locked by it. How can i make sure that after unloading it, also unlocks the file.

    L R 2 Replies Last reply
    0
    • N nitin_ion

      string fileToLoad = @"C:\Users\nitin.jain\Documents\visual studio 2012\Projects\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
      string migrationStepsDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClassLibrary1.dll");
      AppDomainSetup appDomainSetup = new AppDomainSetup() { PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory, ShadowCopyFiles="true" };
      Evidence evidence = AppDomain.CurrentDomain.Evidence;

              AppDomain appDomain = AppDomain.CreateDomain("ClassLibrary1", evidence, appDomainSetup);
      
              //WORKING
              Assembly assembly = Assembly.LoadFile(fileToLoad);
      
              //This part works well
              Type type = assembly.GetType("ClassLibrary1.Class1");
              object foo = Activator.CreateInstance(type);
              MethodInfo methodInfo = type.GetMethod("CheckAdress");
              methodInfo.Invoke(foo, new object\[\] { "navdeep" });
              AppDomain.Unload(appDomain);
              Console.WriteLine("App domain unloaded");
              Console.ReadLine();
      

      This code is in console application and it works fine in the sense that it loads and unload the assembly in the domain. But when my console is running and i try to delete the dll it says that dll is opened in console application. So it is locked by it. How can i make sure that after unloading it, also unlocks the file.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Assembly assembly = Assembly.LoadFile(fileToLoad);

      The assembly object is still in memory, so it is most likely holding an open handle to the file. You could try to dispose of it to see if that releases the file.

      1 Reply Last reply
      0
      • N nitin_ion

        string fileToLoad = @"C:\Users\nitin.jain\Documents\visual studio 2012\Projects\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
        string migrationStepsDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClassLibrary1.dll");
        AppDomainSetup appDomainSetup = new AppDomainSetup() { PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory, ShadowCopyFiles="true" };
        Evidence evidence = AppDomain.CurrentDomain.Evidence;

                AppDomain appDomain = AppDomain.CreateDomain("ClassLibrary1", evidence, appDomainSetup);
        
                //WORKING
                Assembly assembly = Assembly.LoadFile(fileToLoad);
        
                //This part works well
                Type type = assembly.GetType("ClassLibrary1.Class1");
                object foo = Activator.CreateInstance(type);
                MethodInfo methodInfo = type.GetMethod("CheckAdress");
                methodInfo.Invoke(foo, new object\[\] { "navdeep" });
                AppDomain.Unload(appDomain);
                Console.WriteLine("App domain unloaded");
                Console.ReadLine();
        

        This code is in console application and it works fine in the sense that it loads and unload the assembly in the domain. But when my console is running and i try to delete the dll it says that dll is opened in console application. So it is locked by it. How can i make sure that after unloading it, also unlocks the file.

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        It doesn't look like you've loaded that assembly in the new app domain, hence it will be locked for the duration that your application is running. You can't unload an assembly per se, but you can unload the appdomain which its loaded in. Have a look at the method AppDomain.CreateInstanceAndUnwrap to instantiate types in a different domain.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups