By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

How to subclass System.Runtime.Serialization.Formatter?

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3f********@news.swissonline.ch...
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

Nov 15 '05 #2

P: n/a
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[].

--
William Stacey, MVP

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3f********@news.swissonline.ch...
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

Nov 15 '05 #3

P: n/a
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.

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3f********@news.swissonline.ch...
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

Nov 15 '05 #4

P: n/a
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

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.