Subscribe to UMDTAOC        RSS Feed

Extension Me Serialize

Icon 1 Comments
Welcome to the newly founded UMDTAOC.

This entry will focus on extending ISerializable objects with a custom "Serialize" method in C#. ISerializable objects do not have a "Serialize" method. Note: this entry assumes the reader knows what an extension method is, how they are created, how they are used, as well as general knowledge of serialization.

Lets begin by viewing the implementation:
public static class ISerializableSerialization
	public static Stream Serialize<T>(this T @this, String path) where T : ISerializable
		var stream = new FileStream
			FileSystemRights.CreateFiles | FileSystemRights.Read,

		BinaryFormatter formatter = new BinaryFormatter();
		formatter.Serialize(stream, @this);
		stream.Position = 0;
		return stream;

Now lets examine this code:
  • We name the class "ISerializableSerialization" as that is what this method is for: serializing ISerializable objects.
  • We constrain the object's instance type to ISerializable with where T : ISerializable
  • We use a FileStream, using the given path, with which we will serialize our ISerializable object.
  • We use the ol' BinaryFormatter to actually do the serialization of the object.
  • We pass in the FileStream instance along with @this (The object to be serialized) to BinaryFormatter's Serialize method.
  • In this case, we want to allow the user to use the stream (perhaps for..DEserialization?) so, we set the Position in Stream back to zero and return the Stream.

Now lets use it!
ObjectDerivedFromISerializable serializable = new ObjectDerivedFromISerializable();
FileStream stream = serializable.Serialize("%Path To Desktop%");

Here we instantiate a class that (obviously, by its name) derives from the ISerializable interface and named it serializable. We then declare a FileStream object as stream and assign it the return value of serializing serializable. We are passing in a mock form of the path to the desktop to the Serialize method. We then close the stream. Our ISerializable object is now serialized into a file on the desktop!

There are other ways of implementing this method as well:

  • Serialize to a MemoryStream: just use this var stream = new MemoryStream(8) instead of the FileStream.
  • Serialize and Encrypt: -reserved for future topic(s).

NOTE: to derive from the ISerializable interface, the class must override the GetObjectData(SerializationInfo,StreamingContext) method. To deserialize, the class must implement the deserialization constructor ClassName(SerializableInfo,StreamingContext).

I hope this entry finds you well! Happy Coding and don't hesitate to critize :) -well, the content of this entry I mean..

1 Comments On This Entry

Page 1 of 1


09 June 2011 - 04:32 AM
Nicely done! You could have submitted this as a tutorial as well!
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2022

15161718 19 2021

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Looking for a short-term side project

Know of someone needing a custom solution to a technical problem? message me here or view my linkedin profile here.