Loading and unloading assemblies using reflection
-
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.
-
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.
-
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.
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.