1

嗨,我正在将 dll 加载到另一个域中,加载到该域时它工作正常,但是当我想通过代理对象从该域获取一些信息时,它给了我异常下面是审查代码是否有任何错误步骤?

 public class AssemblyProxy
    {
        System.Type[] _types;

    public System.Type[] GetTypes() 
    {
        return _types;
    }

    public string FullName { get; set; }

    public void LoadAssembly(string path)
    {
        try
        {
            Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

            AppDomain TestDomain = AppDomain.CreateDomain("AssemblyDomain", evidence, AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFullPath(path), true);

            Proxy _asmProxy = (Proxy)TestDomain.CreateInstanceFromAndUnwrap(AppDomain.CurrentDomain.BaseDirectory+"Common.dll", typeof(Proxy).FullName);

            _asmProxy.LoadAssembly(path);

            FullName = _asmProxy.FullName;
            _types = _asmProxy.GetTypes(); //Here i got Exception [Can not load file or assembly]

            AppDomain.Unload(TestDomain);
        }
        catch (Exception ex) 
        {

        }

    }

}

class Proxy : MarshalByRefObject
{
    System.Type[] _types;

    public string FullName { get; set; }

    public System.Type[] GetTypes() 
    {
        return _types;                    
    }

    public void LoadAssembly(string path) 
    {
        System.Reflection.Assembly _assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path));
        _types = _assembly.GetTypes();
        FullName = _assembly.FullName;
    }
}

我得到的例外是:

无法加载文件或程序集

4

2 回答 2

1

我解决这个问题的方法是在 AppDomain 的上下文中调用 LoadFrom (而不是 Load) :

sDomain = AppDomain.CreateDomain(DOMAIN_NAME);
sDomain.DoCallBack(AppDomainCallback);

// runs in the context of the AppDomain
private void AppDomainCallback()
{
  Assembly assembly = Assembly.LoadFrom(mAssemblyName);
}
于 2013-09-24T07:04:39.967 回答
0

我通过阅读以下博客文章解决了这个问题:在我的情况下,问题是我正在从新域返回 System.Type 对象,这是不允许的,您可以从代理对象返回字符串,但不能从 System.Type 对象返回

关联

于 2013-09-24T09:25:33.157 回答