如何在AppDomain之间传递对象?试了好久,一直没弄出来, 大家帮帮忙啊

解决方案 »

  1.   

    对象必须是可以系列化(Serializable),或继承于MarshalByRefObject。
      

  2.   

    我写的类已经是(Serializable),也继承了MarshalByRefObject。我也用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null);创建得到了对象代理,但是得到的对象不是clsName的类型,
    现在的问题是怎么把对象obj, 转化成字符串clsName代表的类型?
      

  3.   

    可能是我没说清楚,是这样的下面是我的跨域类的基类
    [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的具体类型序列化或存入另一个域,最后在另一个域里都不能够得到序列化的对象或存入域的值
      

  4.   


    我先用object obj = domain.CreateInstanceFromAndUnwrap(dllName, clsName, true, bindings, null, initParams, null, null, null); 
    得到一个特定具体类的对象 
    然后调用ReflectAssemblyTypes方法...
    根据这段描述,你已经在AppDomain之间传递一个BotermCapability对象了。如果问题是关于ReflectAssemblyTypes(),你要从ReflectAssemblyTypes()中找问题。
    猜不出ReflectAssemblyTypes要做什么。但建议你认真考虑该函数是否必要。
      

  5.   

    [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); //出错