简体   繁体   中英

ShadowCopy assemblies

I have a problem and I really have no clue why it doesn't work. I've read a lot of tutorials (also on stackoverflow) and still nothing.

My goal is to use reflection on some .dll files (they are not use by any program yet) and get inheritance types, methods, constructors etc. Everything works correctly but the problem is that dlls are locked and cannot be deleted until I turn off the program. This is part of my code where I try to resolve the problem.

var apds = new AppDomainSetup();
apds.ApplicationName = "MyAssemblies";
Evidence adevidence = AppDomain.CurrentDomain.Evidence;

AppDomain apd = AppDomain.CreateDomain("newdomain", adevidence, apds);
apd.AppendPrivatePath("Assemblies");
apd.SetCachePath("C:\\Cache");
apd.SetShadowCopyFiles();
foreach (var type in apd.Load(AssemblyName.GetAssemblyName(file.Path)).GetTypes())
{
      foreach (var inherits in GetInheritanceHierarchy(type))
      { //rest is ok

I know I use some deprecated methods but it's one of the try. Dlls are succesfully copied into cache directory but they seems to be loaded into current domain too. Where's the problem? Thanks in advance.


I modified my code to and use Loader class but i still have locked files.

class Loader : MarshalByRefObject
{
    public  Assembly assembly;
    public void LoadAssembly(string path)
    {
        assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));
    }
    public Types[] getTypes()
    {
        return assembly.getTypes();
    }
}
//...
if (file.Type == ".dll")
{
     var apds = new AppDomainSetup
     {
           ApplicationName = "MyAssemblies",
           ApplicationBase = Path,
           ShadowCopyFiles = "true",
           ShadowCopyDirectories = Path
     };
     AppDomain apd = AppDomain.CreateDomain("newdomain", null, apds);
     Loader loader = (Loader)apd.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
     loader.LoadAssembly(file.Path);           
     foreach (var type in loader.getTypes())
     {
           foreach (var inherits in GetInheritanceHierarchy(type))
//...

Any idea?

You can load your assembly in the following way.

var types = Assembly.Load(File.ReadAllBytes("YourAssembly.dll")).GetTypes();

Now you can extract the types from the assembly, and you can delete the assembly while the application is still running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM