加载DLL到另一个域例外 [英] Load dll into another domain exception
本文介绍了加载DLL到另一个域例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我加载的dll到另一个领域,它在加载到该域工作正常,但是当我想从通过代理对象域的一些信息也给了我异常下面是code审查是否有任何错误的一步???
公共类AssemblyProxy
{
的System.Type [] _types;
公众的System.Type [] GetTypes()
{
返回_types;
}
公共字符串全名{获得;组; }
公共无效LoadAssembly(字符串路径)
{
尝试
{
证据证据=新证据(AppDomain.CurrentDomain.Evidence);
AppDomain中TESTDOMAIN = AppDomain.CreateDomain(AssemblyDomain,证据,AppDomain.CurrentDomain.BaseDirectory,System.IO.Path.GetFullPath(路径),TRUE);
代理_asmProxy =(代理)TestDomain.CreateInstanceFromAndUnwrap(AppDomain.CurrentDomain.BaseDirectory +Common.dll的typeof(代理).FullName);
_asmProxy.LoadAssembly(路径);
全名= _asmProxy.FullName;
_types = _asmProxy.GetTypes(); //这里我得到异常[无法加载文件或程序集]
AppDomain.Unload(TESTDOMAIN);
}
赶上(例外前)
{
}
}
}
级代理:MarshalByRefObject的
{
的System.Type [] _types;
公共字符串全名{获得;组; }
公众的System.Type [] GetTypes()
{
返回_types;
}
公共无效LoadAssembly(字符串路径)
{
System.Reflection.Assembly _Assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(路径));
_types = _assembly.GetTypes();
全名= _assembly.FullName;
}
}
我得到的例外是:
无法加载文件或程序集
解决方案
我解决这个问题的方法是通过调用LoadFrom(不加载)和应用程序域的上下文:
sDomain = AppDomain.CreateDomain(域名);
sDomain.DoCallBack(AppDomainCallback);
//运行在的AppDomain的背景下
私人无效AppDomainCallback()
{
装配装配= Assembly.LoadFrom(mAssemblyName);
}
Hi I am loading dll into another domain, it works fine when loaded into that domain but when i want some information from that domain through proxy object it gives me exception below is the code for review is there any wrong step ???
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;
}
}
The exception I get is:
Can not load file or assembly
解决方案
The way I solved this problem was by calling LoadFrom (not Load) and in the context of the AppDomain:
sDomain = AppDomain.CreateDomain(DOMAIN_NAME);
sDomain.DoCallBack(AppDomainCallback);
// runs in the context of the AppDomain
private void AppDomainCallback()
{
Assembly assembly = Assembly.LoadFrom(mAssemblyName);
}
这篇关于加载DLL到另一个域例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文