简体   繁体   English

ShadowCopy程序集

[英]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. 我已经阅读了很多教程(也在stackoverflow上),仍然一无所获。

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. 我的目标是在某些.dll文件上使用反射(它们尚未被任何程序使用)并获取继承类型,方法,构造函数等。一切正常,但问题是dll被锁定并且无法关闭,直到我关闭该程序。 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. Dll已成功复制到缓存目录中,但它们似乎也已加载到当前域中。 Where's the problem? 哪里出问题了? Thanks in advance. 提前致谢。


I modified my code to and use Loader class but i still have locked files. 我修改了代码,并使用Loader类,但是我仍然锁定了文件。

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. 现在,您可以从程序集中提取类型,并且可以在应用程序仍在运行时删除程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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