473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

custumize serialization & CollectionBase inherited class

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.Collection s.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));

}

}
Nov 15 '05 #1
2 3945
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]
- mv*@spam.guard.caspershouse.com

"wael radwan" <wa*********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP12.phx.gbl...
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.Collection s.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));

}

}

Nov 15 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O7**************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"wael radwan" <wa*********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP12.phx.gbl...
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.Collection s.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));

}

}


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
2
by: Just D. | last post by:
All, 1. Did anybody write Serialization/Deserialization of some custom class derived from the CollectionBase class? The custom class is like a container of many different simple classes, each...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
10
by: SStory | last post by:
My app is near completed for the basic feature of version 1.0. I have an extensive object model and I now want to persist my objects using serialization. I have chosen binaryformatter to...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
2
by: GoCoogs | last post by:
I'm trying to count how many items are in a dynamic collection. This is the code I have so far. *** Begin Code *** Public Class Rule Private _rulevars As RuleVarsCollection Private _rulename...
2
by: Stefan Hoffmann | last post by:
hi, as i'm farely new to C#, i have some problem of creating a shallow copy of a class inherited of CollectionBase. As far as i see, i just need to copy the CollectionBase.List using the...
0
by: LIJO CHEERAN | last post by:
Hello friends I am trying to study about CollectionBase. I have inherited CollectionBase in the class TheCollection.cs. I am using the “TheCollection. in an aspx page to store objects...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.