W
wael radwan
how to customize the serialization of a collection that inherited form
CollectionBase i.e making the class implementing ISerializable interface
because when deserializing using the desrializing constructor(the one that
has the parametr list as GetObjectData method) i can't using assiment to
List refrence because it is read only
this code could be helpful to undersatnd
[Serializable()]
public class group:System.Collections.CollectionBase//,ISerializable
{
public void GetObjectData(SerializationInfo info,StreamingContext ctxt)
{
info.AddValue("List",List);
}
group(SerializationInfo info,StreamingContext ctxt)
{
List=info.GetValue("List",typeof(System.Collections.IList);
}
public void Add(Lib.Employee emp)
{
List.Add(emp);
}
public Employee this [int index]
{
set
{
List.Add(value);
}
get
{
return (Employee)List[index];
}
}
}
[Serializable()]
public class Employee:ISerializable
{
public int ID;
public string Name;
public Employee()
{
ID=0;
Name=null;
}
public Employee(string name,int id)
{
ID=id;
Name=name;
}
public void GetObjectData(SerializationInfo info,StreamingContext con)
{
Console.WriteLine("Saving employee object");
info.AddValue("ID",ID);
info.AddValue("Employee Name",Name);
}
public Employee(SerializationInfo info,StreamingContext ctxt)
{
Console.WriteLine("using constructor");
ID=(int)info.GetValue("ID",typeof(int));
Name=(string)info.GetValue("Employee Name",typeof(string));
}
}
CollectionBase i.e making the class implementing ISerializable interface
because when deserializing using the desrializing constructor(the one that
has the parametr list as GetObjectData method) i can't using assiment to
List refrence because it is read only
this code could be helpful to undersatnd
[Serializable()]
public class group:System.Collections.CollectionBase//,ISerializable
{
public void GetObjectData(SerializationInfo info,StreamingContext ctxt)
{
info.AddValue("List",List);
}
group(SerializationInfo info,StreamingContext ctxt)
{
List=info.GetValue("List",typeof(System.Collections.IList);
}
public void Add(Lib.Employee emp)
{
List.Add(emp);
}
public Employee this [int index]
{
set
{
List.Add(value);
}
get
{
return (Employee)List[index];
}
}
}
[Serializable()]
public class Employee:ISerializable
{
public int ID;
public string Name;
public Employee()
{
ID=0;
Name=null;
}
public Employee(string name,int id)
{
ID=id;
Name=name;
}
public void GetObjectData(SerializationInfo info,StreamingContext con)
{
Console.WriteLine("Saving employee object");
info.AddValue("ID",ID);
info.AddValue("Employee Name",Name);
}
public Employee(SerializationInfo info,StreamingContext ctxt)
{
Console.WriteLine("using constructor");
ID=(int)info.GetValue("ID",typeof(int));
Name=(string)info.GetValue("Employee Name",typeof(string));
}
}