Download the C# project for the serializer from here
I needed a small footprint, highly performant binary, reflection based serializer for the project I am working on. I couldn't find anything that fitted my needs on the web, so I built one and offer it here
for anyone else who has similar requirements.
With the introduction of shared classes between Silverlight and .NET, it can be very useful to use serialization to pass data to and from WCF services - the only in built way to do this is using the
for Plain Old CLR Objects (POCO). XML serialization is very slow however and produces very large strings, plus you have to declare the
for everything that might be included in the object graph.
I looked around at a number of other serializers, but none of them worked the way I wanted or in many cases worked at all!
So I wrote a new serializer which uses reflection to inspect the object graph being serialized and can store all of the
read/write properties and fields of the object. It stores the data in a binary format and uses tokenization to reduce the size of the outputted byte array.
You could further compress this array for transmission (I use a ZIP library in the project that this serializer comes from).
- Caches property information for rapid performance
- Stores only properties which are different from a vanilla instance created using a parameterless constructor
- Doesn't store typenames when they can be inferred from property types (but does support setting a property to a sub classed item, in which case the type is stored)
- Tokenizes property and field names to reduce space
- Tokenizes stored types to save space
- Does not require definition of known types
You use the serializer either directly by calling the
(item) method or by using the extension method
() on any object. To rehydrate the object, you call
passing in a previously created byte array.
There are a couple of limitations (which could be addressed easily in the code if you need to):
- Only serializes 255 properties per object (though the object can have more properties than that, it uses a byte to say how many properties need to be serialize)
- Supports up to 65535 types in the object graph
- Supports up to 65535 unique property/field names in the graph
var bytes = AnyObject.ToBytes();
var otherWay = Serializer.Serialize(AnyObject);
MyItem anInstanceOfMyItem = Serializer.Deserialize(bytes) as MyItem;
One interesting thing to note is that the code ran up to 4 times faster when I converted the enumeration of reflected
for a class into an Array, clearly the cost of Next on the original enumeration is significant and really proves the point about caching rather than calling every time.
The serializer ended up using Stacks and thread statics to handle recursion (when an item needs to store a serialized item as a body) - this would probably be better off as a utility instance, but at least it means that the class is
and can declare its own extension methods!
I use this to support Agent Pattern development where UI agents get sent to run in a WCF service, collect information and then return to the client.
The latest version and documentation are available on my blog