7

我正在从新的 AppDomain 中的特定“扩展”目录中加载所有 DLL,以从中获取一些与反射相关的信息。

这就是我正在尝试的:

我在我的解决方案中创建了一个新库AssemblyProxy,它只有这个类:

public class AssemblyProxy : MarshalByRefObject
{
    public Assembly LoadFile( string assemblyPath )
    {
      try
      {
        return Assembly.LoadFile( assemblyPath );
      }
      catch
      {
        return null;
      }
    }
}

我确保这个 DLL 存在于我的“扩展”目录中。然后我使用以下代码将所有程序集从“扩展”目录加载到新的AppDomain.

foreach( string extensionFile in Directory.GetFiles( ExtensionsDirectory, "*.dll" ) )
{
        Type type = typeof( AssemblyProxy.AssemblyProxy );
        var value = (AssemblyProxy.AssemblyProxy) Domain.CreateInstanceAndUnwrap(
            type.Assembly.FullName,
            type.FullName );

        var extensionAssembly = value.LoadFile( extensionFile );

        types.AddRange( extensionAssembly.GetTypes() );
}

某些 DLL 确实加载成功,但在某些 DLL 上会抛出如下异常:

Could not load file or assembly 'Drivers, Version=2.3.0.77, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

编辑:新的 AppDomain 中不会引发异常。DLL 在新的 AppDomain 中成功加载。一旦程序集引用返回到主/调用 AppDomain,就会引发异常。主/调用 AppDomain 是否尝试在收到引用时自行加载程序集?

谢谢。

4

1 回答 1

3

您不应该Assembly从新对象返回一个对象,AppDomain因为这只有在您AppDomain可以访问那些它没有的程序集时才有效,因为这些程序集位于以下目录中:

避免这种情况的一种方法是遵循 leppie 的评论,并且:

  1. 创建一个序列化类型,其中包含您需要从Assembly对象获得的最少信息。
  2. 将此类型添加到两个 AppDomain 都可以访问的新程序集。最简单的方法是将其添加到 GAC。

另一种方法是使用Mono.Cecil而不是System.Reflection. Mono.Cecil 将允许您在不加载程序集的情况下检查它们。对于一个非常简单的示例,请查看此答案的后半部分。

于 2013-01-04T10:47:16.330 回答