How to subclass System.Runtime.Serialization.Formatter?

A

Andreas Huber

Hello there

I need to serialize/deserialize some pretty simple data structures (no
inheritance, few has relationships, ~20 classes) in three formats. One is
XML (structure is not important as long as it's human-readable and
modifiable), and two are proprietary binary formats. For XML I'll use the
SoapFormatter, no sweat.
For the two other formats I would like to implement my own formatters.
Problem is the docs on implementing your own formatter are pretty thin and
Google does not seem to find anything useable either.

- Has anyone implemented a custom formatter in a real-world application?
- Is there a book covering custom formatters in depth?
- How do I override the abstract properties SurrogateSelector, Binder, and
Context?
- Where does the FormatterServices class utility come into the equation?

Thanks,

Andreas
 
N

Nicholas Paldino [.NET/C# MVP]

Andreas,

I've never implemented a custom formatter for a real-world app, but I'll
help you where I can.

As far as I know, there is not a book that covers formatters in depth.
It is one of those areas in the framework that doesn't have a lot of light
shed on it.

The FormatterServices utility class is used to provide utility functions
for serialization. For example, if you want to create an instance of an
object, without using a constructor, then you would use the static
GetUninitializedObject method on the FormatterServices class to get it.
Subsequently, you can use the static PopulateObject members method to
populate the fields in the object without using reflection on your own.

I'm looking on how to override the three properties that you have
mentioned. Send me a separate email, and I will let you know if I have come
up with anything.

Hope this helps.
 
W

William Stacey

1) Use the binaryformatter for one option.
2) If your structs (or classes) are simple (primitive types) and you add the
LayoutKind.Sequential or Explicit then you can use the Marshal class to make
a byte[] and marshal back from byte[].
 
D

Dave

I implemented custom formatters and a binder in my app - it works pretty
well.

Jeffrey Richter wrote a series of articles in MSDN that covers these topics
fairly well.
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

You implement a custom formatter when you want to provide special formatting
when serializing/deserializing the data. One use for this is to ignore
missing fields when deserializing data (e.g. a newer version has dropped
some fields and added new fields). Another use is when the same field is
present but the datatype has changed - you can convert the original data to
the new required type.

Normally the runtime will throw an exception when the serialized data was
originally generated by an object of one version and is being deserialized
into an object of a different version. You can write a Binder that will
override this default behavior and force it to bind the data to a different
version of that type.

I don't override the Context. It tells you where the stream came from/is
going to. You may find this useful if writing a formatter that needs to
handle the data differently based on the context.

You can use a surrogate when you don't have access to the source code and
want to apply custom formatting. I haven't used this so I can't add much
detail to this.

The FormatterServices class provides useful methods such as getting an array
of all fields marked as serializable for a given type. You need this list
when you write a custom formatter.
 
A

Andreas Huber

Dave, William, Nicholas

Thanks a lot for your answers! Jeffrey Richters' articles look especially
helpful. I wonder why Google did not find these...

Regards,

Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top