如何在AppDomain之间传递对象? 如何在AppDomain之间传递对象?试了好久,一直没弄出来, 大家帮帮忙啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 对象必须是可以系列化(Serializable),或继承于MarshalByRefObject。 我写的类已经是(Serializable),也继承了MarshalByRefObject。我也用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null);创建得到了对象代理,但是得到的对象不是clsName的类型,现在的问题是怎么把对象obj, 转化成字符串clsName代表的类型? 可能是我没说清楚,是这样的下面是我的跨域类的基类[Serializable]public class BotermCapability:MarshalByRefObject{ public void ReflectAssemblyTypes(string dllName, ref AppDomain domain) { try { StringBuilder currentType = new StringBuilder(); Assembly asm = AppDomain.CurrentDomain.Load(dllName); Type[] types = asm.GetTypes(); //domain.SetData(dllName, types.Clone()); using (FileStream fs = new FileStream(@"C:\bean.dat", FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, asm.GetName(true)); fs.Close(); } } catch(Exception ex) { throws } } }每个具体类都继承了上面的 类我先用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null);得到一个特定具体类的对象然后调用ReflectAssemblyTypes方法,将对象所在Assembly的具体类型序列化或存入另一个域,最后在另一个域里都不能够得到序列化的对象或存入域的值 我先用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null); 得到一个特定具体类的对象 然后调用ReflectAssemblyTypes方法...根据这段描述,你已经在AppDomain之间传递一个BotermCapability对象了。如果问题是关于ReflectAssemblyTypes(),你要从ReflectAssemblyTypes()中找问题。猜不出ReflectAssemblyTypes要做什么。但建议你认真考虑该函数是否必要。 [Serializable] public class BotermCapability:MarshalByRefObject { //功能: 把对象所在程序集里的所有System.Type保存下来,让另外的domain也可以使用这些System.Type对象 public AppDomain ReflectAssemblyTypes(string dllName) { try { StringBuilder currentType = new StringBuilder(); Assembly asm = AppDomain.CurrentDomain.Load(dllName); Type[] types = asm.GetTypes(); AppDomain.CurrentDomain.SetData(dllName, types); } catch(Exception ex) { throws } } } AppDomain appDomain = ((Com.Company.Project.Core.Contract.PluginCapability)testObj).ReflectAssemblyTypes(dllName, outFile);Type[] beanType = (Type[])((System.AppDomain)appDomain).GetData(dllName); //出错 怎么统计字段相同值的记录? 能否指定系统必备组件的安装顺序 软件开机启动并全屏显示 MySqlConnection 连接远程数据库最是出错 急在线等 很实用!! 格式转换问题 【在线】如何迭代继承了DictionaryBase类的集合类? 关于设计QQ资料...懂的人进来下 极其郁闷WIN2003环境下输入法切换造成.NET开发环境死机 怎么对GridView进行打印! 请问自己做一个控件,当自定义的属性改变时触发一自定义的事件,这个自定义的事件该如何定义? C# winform过滤文本文件中的重复字符串 关于try catch的问题
现在的问题是怎么把对象obj, 转化成字符串clsName代表的类型?
[Serializable]
public class BotermCapability:MarshalByRefObject
{
public void ReflectAssemblyTypes(string dllName, ref AppDomain domain)
{
try
{
StringBuilder currentType = new StringBuilder();
Assembly asm = AppDomain.CurrentDomain.Load(dllName);
Type[] types = asm.GetTypes();
//domain.SetData(dllName, types.Clone());
using (FileStream fs = new FileStream(@"C:\bean.dat", FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, asm.GetName(true));
fs.Close();
}
}
catch(Exception ex)
{
throws
}
}
}每个具体类都继承了上面的 类我先用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null);得到一个特定具体类的对象然后调用ReflectAssemblyTypes方法,将对象所在Assembly的具体类型序列化或存入另一个域,最后在另一个域里都不能够得到序列化的对象或存入域的值
我先用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null);
得到一个特定具体类的对象
然后调用ReflectAssemblyTypes方法...
根据这段描述,你已经在AppDomain之间传递一个BotermCapability对象了。如果问题是关于ReflectAssemblyTypes(),你要从ReflectAssemblyTypes()中找问题。
猜不出ReflectAssemblyTypes要做什么。但建议你认真考虑该函数是否必要。
public class BotermCapability:MarshalByRefObject
{
//功能: 把对象所在程序集里的所有System.Type保存下来,让另外的domain也可以使用这些System.Type对象
public AppDomain ReflectAssemblyTypes(string dllName)
{
try
{
StringBuilder currentType = new StringBuilder();
Assembly asm = AppDomain.CurrentDomain.Load(dllName);
Type[] types = asm.GetTypes();
AppDomain.CurrentDomain.SetData(dllName, types);
}
catch(Exception ex)
{
throws
}
}
} AppDomain appDomain = ((Com.Company.Project.Core.Contract.PluginCapability)testObj).ReflectAssemblyTypes(dllName, outFile);Type[] beanType = (Type[])((System.AppDomain)appDomain).GetData(dllName); //出错