views:

37

answers:

1

XmlSerialization creates a serializer proxy for each class. The proxy resides in a different assembly so it can serialize only public fields.

DataContract serialization can serialize private fields too. Does it mean it uses reflection? Isn't it slower than using a proxy (except for the first time)?

+1  A: 

This protobuf-net page shows the performance of DataContractSerializer to be significantly better than XmlSerializer. Of course, you should always test with your own data, but if you are looking to replace XmlSerializer, you will most likely find DataContractSerializer to be a performance improvement.

I'm not sure how DataContractSerializer is implemented internally, but generally serializers are highly optimized. This is especially true for DataContractSerializer since it is a big part of the performance picture for WCF. It is not uncommon for a serializer to generate MSIL code on the fly. When this is done, DynamicMethod allows you to (surprisingly!) bypass visibility checks (see MSDN), so it is possible to access private fields without reflection.

From MSDN:

Given sufficient security permissions, a serialization engine implemented using dynamic methods can access private and protected data to enable serialization of objects not authored by the engine creator.

binarycoder