Case:
Pass a unserializable parameter cross AppDomain.
Following is some method which i want to call in remote domain;
public RemoteClass
{
public Test(Class1 obj);
public Test(List<Class1> obj);
}
Define:
Class1 : un-serializable
[Serializable]
Class2 : Class1 , ISerializable //mark Class2 serializable
{
//.....
}
Following code used to test:
using (MemoryStream s = new MemoryStream())
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(s, obj);
}
Test result:
obj result
Class1 obj=new Class1(); exception
Class1 obj=new Class2(); success
List<Class1> obj=new List<Class1); exception when obj contain some element;
obj.Add(new Class1();
List<Class1> obj=new List<Class1); exception when obj contain some element; //how???
obj.Add(new Class2();
List<Class2> obj=new List<Class2); success;
obj.Add(new Class2();
Class1 is not a serializable calss, and I cannot modify it, so I have define Class2 which inherits from Class1 and implements ISerializable. I can pass an instance of Class2 when a method needs a instance of Class1 in test result and this solution is successful, however for List<Class1> this does not work.
How about converting the
List<Class1>instance into a listList<Class2>(the following should work as long as you declare a suitable constructor onClass2):You should then be able to serialise
List<Class2>just fine.You find that you need to convert your serialised list back into a
List<Class>before you can use it anywhere that accepts aList<Class1>– a variation on the above should work just as well in reverse.