Jump to content
Sign in to follow this  
  • entries
    7
  • comments
    7
  • views
    3847

Serialization code

Sign in to follow this  
dot

299 views

Figured it might be interesting to post my serialization related code with some explainations.

Documentation/assertions/error checking are removed and replaced with comments if necessary for this snipplet.

First the attribute which helps to identify which field to serialize and what name to serialize as.

public sealed class SerializeAsAttribute : Attribute {
public string NameToUse {
get { return name_; }
}

public SerializeAsAttribute(string name) {
name_ = name;
}

private readonly string name_;
}



Next up, the helper

public sealed class SerializationHelper {
internal struct MarkedField {
internal FieldInfo field_;
internal SerializeAsAttribute attribute_; // stored so that I don't have to do another runtime query on field.
}

private static MarkedField[] GetSerializableFields(ISerializable o) {
List list = new List();

Type t = o.GetType();
FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

foreach (FieldInfo field in fields) {
object[] attributes = field.GetCustomAttributes(typeof(SerializeAsAttribute), false);
if (attributes != null && attributes.Length > 0) {
MarkedField marked = new MarkedField();
marked.field_ = field;
marked.attribute_ = attributes[0] as SerializeAsAttribute;

list.Add(marked);
}
}

return list.ToArray();
}

public static void Serialize(SerializationInfo info, ISerializable o) {
MarkedField[] fields = GetSerializableFields(o);

foreach (MarkedField field in fields) {
info.AddValue(field.attribute_.NameToUse, field.field_.GetValue(o));
}
}

public static void Deserialize(SerializationInfo info, ISerializable o) {
MarkedField[] fields = GetSerializableFields(o);

foreach (MarkedField field in fields) {
field.field_.SetValue(o,
info.GetValue(field.attribute_.NameToUse, field.field_.FieldType));
}
}
}



And a sample usage

[Serializable]
public sealed class TestObject : ISerializable {
#region ISerializable Members

public TestObject(SerializationInfo info, StreamingContext context) {
SerializationHelper.Deserialize(info, this);
}

public void GetObjectData(SerializationInfo info, StreamingContext context) {
SerializationHelper.Serialize(info, this);
}

#endregion

[SerializeAs("Name")]
public string Name;

[SerializeAs("Number")]
public int Number;

[SerializeAs("RealNumber")]
public float RealNumber;
}



Now, probably the most interesting point to take note is that, i can set the values of private fields via runtime reflection. Bug/loophole, or feature? I'll leave that for the reader to decide.

PS. Reflection rocks!
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!