Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Serializing, Consuming, and Binding Generics in .NET 2.0 : Page 2

Generics are a powerful new feature in .NET 2.0, but it's not so obvious how to use them as return values from Web services, or how to bind them to controls. Find out how to serialize, data bind, and consume generic collections in ASP.NET 2.0


advertisement
How to Serialize a Generic Class
Because of the built-in support provided by the .NET Framework 2.0 for handling generics, you can take advantage of XML serialization to serialize and deserialize particular specializations of the generics type by using the following code.

XmlSerializer serializer = new XmlSerializer (typeof(Compare<int, int>));

To serialize and deserialize a generic class, you just need to follow two steps.

  • Create an instance of the XmlSerializer class and pass the type of the generic type to be serialized as an argument
  • Invoke the Serialize() or Deserialize() method of the XmlSerializer class, passing in the object to be serialized or deserialized

using System; using System.Collections; using System.Xml.Serialization; [XmlRoot("KeyValuePair")] public class KeyValuePair<KeyType, ValueType> { private KeyType _key; private ValueType _value; public KeyValuePair() {} public ValueType Value { get{return _value; } set{_value = value;} } public KeyType Key { get{return _key; } set{_key = value; } } }

The preceding code declares a class named KeyValuePair that accepts two runtime types; one for the key element and another one for the value element. As part of the class declaration, there is also an XmlRoot attribute that ensures the root element of the serialized XML document is named "KeyValuePair." The code defines two public properties named Value and Key that simply set or get values from the private variables _value and _key respectively. Keeping the KeyValuePair class definition in mind, Listing 1 discusses the code required to serialize or deserialize an instance of the KeyValuePair class using XML serialization.

Listing 1 contains two methods named Serialize() and Deserialize(). The Serialize() method starts by declaring a KeyValuePair object with the type parameters set to int and string respectively.



KeyValuePair<int, string> keyVal = new KeyValuePair<int, string>();

Then the code invokes the Key and Value properties of the KeyValuePair object to set the values appropriately.

keyVal.Key = 1; keyVal.Value = "Production";

After doing that, you supply the KeyValuePair object as a parameter to the constructor of the XmlSerializer object, indicating the typed parameters.

XmlSerializer serializer = new XmlSerializer(typeof (KeyValuePair<int, string>));

That's all there is to serializing a generic type. The rest of the code in Listing 1 is similar to the code examples shown earlier. The Deserialize() method works along the same lines, passing in typed parameters to the constructor of the XmlSerializer and then finally invoking the XmlSerializer.Deserialize() method to deserialize the XML data into an instance of the KeyValuePair object. If you browse to the page using the browser, you will see buttons for invoking the Serialize() and Deserialize() methods. Clicking the "Serialize" button causes the server to create an XML file named KeyValuePair.xml file created in the C:\Data directory containing the XML for the serialized KeyValuePair object. Here's how that file looks.

<?xml version="1.0" encoding="utf-8"?> <KeyValuePair xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema"> <Value>Production</Value> <Key>1</Key> </KeyValuePair>



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap