You need to create a collection that can hold elements only of a specific type.
Create a class that derives from the
System.Collections.CollectionBase
or
System.Collections.DictionaryBase
classes, and implement
type-safe
The
CollectionBase
and
DictionaryBase
classes
provide
CollectionBase is for IList -based collections (such as ArrayList ). Internally, CollectionBase maintains the collection using a standard ArrayList object, which is accessible through the protected property List . DictionaryBase is for IDictionary -based collections (such as Hashtable ). Internally, DictionaryBase maintains the collection using a standard Hashtable object, which is accessible through the protected property Dictionary . The following code shows the implementation of a strongly typed collection (based on the CollectionBase class) to represent a list of System.Reflection.AssemblyName objects.
using System.Reflection;
using System.Collections;
public class AssemblyNameList : CollectionBase {
public int Add(AssemblyName value) {
return this.List.Add(value);
}
public void Remove(AssemblyName value) {
this.List.Remove(value);
}
public AssemblyName this[int index] {
get {
return (AssemblyName)this.List[index];
}
set {
this.List[index] = value;
}
}
public bool Contains(AssemblyName value) {
return this.List.Contains(value);
}
public void Insert(int index, AssemblyName value) {
this.List.Insert(index, value);
}
}
Both the CollectionBase and DictionaryBase classes implement a set of protected methods with the prefix On *. These methodssuch as OnClear , OnClearComplete , OnGet , OnGetComplete , and so onare intended to be overridden by a derived class and allow you to implement any custom functionality necessary to manage the strongly typed collection. The CollectionBase and DictionaryBase classes call the appropriate method before and after modifications are made to the underlying collection through the List or Dictionary properties.
You need to store a serializable object and its state to a file and then
Use a
formatter
to serialize the object and write it to a
System.IO.FileStream
. When you need to retrieve the object, use the same type of formatter to read the serialized data from the file and deserialize the object. The .NET Framework class library includes the following formatter
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
Using the
BinaryFormatter
and
SoapFormatter
classes, you can serialize an instance of any type that's
Both the
BinaryFormatter
and
SoapFormatter
classes implement the interface
System.Runtime.Serialization.IFormatter
, which defines two
| Important |
To call the Serialize and Deserialize methods of the BinaryFormatter class, your code must be granted the SerializationFormatter element of the permission System.Security.Permissions.SecurityPermission .
To call the
Serialize
and
Deserialize
methods of the
SoapFormatter
class, your code must be granted full trust because the System.Runtime.Serialization.Formatters.Soap.dll assembly in which the
SoapFormatter
class is declared does not allow partially trusted
|
The
BinarySerializationExample
class listed here
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
public class BinarySerializationExample {
public static void Main() {
// Create and configure the ArrayList to serialize
ArrayList people = new ArrayList();
people.Add("Graeme");
people.Add("Lin");
people.Add("Andy");
// Serialize the ArrayList object
FileStream str = File.Create("people.bin");
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(str, people);
str.Close();
// Deserialize the ArrayList object
str = File.OpenRead("people.bin");
bf = new BinaryFormatter();
people = (ArrayList)bf.Deserialize(str);
str.Close();
// Display the contents of the deserialized ArrayList object
foreach (string s in people) {
System.Console.WriteLine(s);
}
}
}
You can use a SoapFormatter class in exactly the same way as shown in the BinarySerializationExample class; all you need to do is replace each instance of BinaryFormatter with SoapFormatter and change the using directives to import the System.Runtime.Serialization.Formatters.Soap namespace. You must also include a reference to the System.Runtime.Serialization.Formatters.Soap.dll assembly when you compile the code. The file SoapSerializationExample.cs in the sample code for this chapter contains an example of how to use the SoapFormatter class.
To
Figure 2.1:
Contents of the people.bin file.
Figure 2.2:
Contents of the people.xml file.