C# question

Started by
7 comments, last by cyansoft 15 years, 4 months ago
I'm a C++ programmer using C#/XNA for the indie game I'm developing because Microsoft's Community Games is too good of an opportunity to pass up. I'm trying to take advantage of XML Serialization and Property Grids for use in my data format/toolset, but putting get/set properties on every memeber is not sitting well with my tendency to publically expose as little of my objects as possible. Is there a way to avoid doint that or do I just need to suck it up so that I don't have to write my own serialization routines and property dialog boxes?
Advertisement
So you want serialization without publicly exposing all members you want to serialize? From what I remember, binary serialization can do that.
NetGore - Open source multiplayer RPG engine
you can implement the ISerializable interface directly, instead of just trusting serialization to do the right thing automagically. Also, you can mark individual items with NonSerialized as appropriate.
All members of the class not marked with [NonSerialzable] will be serialized with a BinaryFormmater. Unfortunately XML serialization requires public properties. So if you don't care about the format of the saved data, use Binary (it will also do nested classes but they must be marked [Serializable] or you will get exceptions. The majority of framework classes are serializable). Otherwise you have the other option of implementing ISerializable as Xai said. I find BinaryFormatter suits most of my needs.

You will also find that after you serialize binary that the names of classes etc are plainly visible in your file if you hex edit or view in notepad. You can nest the stream inside a zip stream and / or encryption before you send it to disc as well.
The properties need to be public on the concrete class, they do not need to be part of the interface at all or they can be read-only in the interface.

You can tag public properties with [Browseable(false)] so they do not show up on the property grid and add more property-grid-friendly properties to replace them.
e.g. One that returns "127.0.0.1" instead of 0100007F.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by Shannon Barber
The properties need to be public on the concrete class, they do not need to be part of the interface at all or they can be read-only in the interface.

You can tag public properties with [Browseable(false)] so they do not show up on the property grid and add more property-grid-friendly properties to replace them.
e.g. One that returns "127.0.0.1" instead of 0100007F.


you can also tackle more complex versions of this desire by writing custom Designer classes, instead of hiding properties.
I usually create dedicated data classes for information that needs to be serialized that are held within classes responsible for logic to reduce the problems of making all the methods public.
Early warning, though I'm not sure what you're planning: At least in XNA 2.0, the 360's framework did not include the BinaryFormatter class or the FormatterServices class necessary to write a good serializer of your own. I haven't re-checked in XNA 3.0 yet, though.
Quote:Original post by Nypyren
Early warning, though I'm not sure what you're planning: At least in XNA 2.0, the 360's framework did not include the BinaryFormatter class or the FormatterServices class necessary to write a good serializer of your own. I haven't re-checked in XNA 3.0 yet, though.


It won't work. The BinaryFormatter is not part of the XNA Framework, but rather part of the main class library. The XBox 360 uses the .NET Compact Framework, which does not have the BinaryFormatter class.

You probably want to use the IntermediateSerializer class to create the XML files to be processed by the Content Processor for your game. Shawn Hargreaves, one of the XNA developers, has several blog postings going into great detail on how to use this class. This one in particular goes into details on how to serialize private members, collections, and controlling the XML output among other things.

This topic is closed to new replies.

Advertisement