custumize serialization & CollectionBase inherited class

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));

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Wael,

You can't pass a type of the interface, because an interface can't be
instantiated. What you have to do is pass the type of the "List" field, and
then it should work.

Hope this helps.
 
W

wael radwan

Nicholas ,

the compiling error was:

Property or indexer 'System.Collections.CollectionBase.List' cannot be
assigned to -- it is read only

typeof return the type it does not instantiate an object
so the problem was about List member is readonly

Nicholas Paldino said:
Wael,

You can't pass a type of the interface, because an interface can't be
instantiated. What you have to do is pass the type of the "List" field, and
then it should work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

wael radwan said:
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));

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top