通过反射从加载的程序集返回Types []时的FileNotFound

时间:2015-06-30 18:14:37

标签: c# dll reflection .net-assembly appdomain

我正在尝试加载插件程序集(放在

  

D:\ xyz \ addin.dll

我的申请表在

  

d:\ MyApp的\ MyApp.exe的

不应该锁定addin文件,以便在应用程序运行时再次复制它的新版本。 为此,我创建了新的应用程序域并加载了包含AssembltLoader类的公共dll,然后调用AssemblyLoader来加载插件并获取其中的所有类型。 Addin dll的依赖项已经放在同一个文件夹中。 现在从addin dll获取类型工作正常,但当我

  

返回assembly.GetTypes();

发生了一件非常奇怪的事情。它执行该行但在退出该函数时会抛出异常“d:\ xyz \ addin.dll”FileNotFound

public class ObjectLoader : IDisposable
{
    public Type[] GetAllTypesFromAssembly(string filePath)
    {
        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
        setup.ShadowCopyFiles = "true";
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;

        AssemblyLoader asemblyLoader = null;

        AppDomain appDomain = AppDomain.CreateDomain(filePath, adevidence, setup);

        appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

        domains.Add(filePath, appDomain);
        object[] parms = { filePath };

        BindingFlags bindings = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public;
        try
        {
            asemblyLoader = (AssemblyLoader)appDomain.CreateInstanceFromAndUnwrap(
            setup.ApplicationBase +
            "Common.dll", "Common.AssemblyLoader", 
            true, bindings, null, parms, null, null, null
            );
        }
        catch (Exception ex)
        {
            throw ex; // new AssemblyLoadFailureException();
        }

        object types = null;
        if (asemblyLoader != null)
        {
            // On following line i am facing the error which occur on following line but when GetAllTypes exits. 
            types = asemblyLoader.GetAllTypes();
         }
         return (Type[])types;
     }
}

internal class AssemblyLoader : MarshalByRefObject, IDisposable
{
    private Assembly assembly = null;
    public object GetAllTypes()
    {
        BindingFlags flags = BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public;
        object types = null;
        if (assembly != null)
        {
            try
            {
                // following line works fine and loads a type (it contains one class)
                types = assembly.GetTypes(); 
            }
            catch (Exception)
            {
                types = new ObjectLoadFailureException();
            }
        }
        else
        {
            types = new AssemblyNotLoadedException();
        }
        return types;
    }
}

以下是例外情况:

  

System.IO.FileNotFoundException was unhandled
HResult=-2147024894
Message=Could not load file or assembly 'AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog==== Pre-bind state information ===
LOG: DisplayName = AddIn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///D:/MyApp/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).

如果我在D:\ xyz \和D:\ MyApp \中复制我的addin.dll,则没有错误。我不能在运行时环境中这样做。

1 个答案:

答案 0 :(得分:0)

您正在将Type个对象编组到主AppDomain中 这会导致CLR加载包含该AppDomain中类型的程序集,从而导致此错误。

您可能根本不应该使用AppDomains。