Other then the speed issues when accessing and hashtable and casting value types, is this really very bad?
[Serializable]
public class Cheat : ISerializable
{
public Cheat() { }
public string Orange { get { return Convert.ToString(_mData["Orange"]); } set { _mData["Orange"] = value; } }
public List<int> Ints { get { return (List<int>)(_mData["Ints"]); } set { _mData["Ints"] = value; } }
#region ISerializable Members
protected Hashtable _mData = new Hashtable();
protected Cheat(SerializationInfo info, StreamingContext context)
{
_mData = (Hashtable)info.GetValue("_mData", typeof(Hashtable));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("_mData", _mData);
}
#endregion
}
By using the
ISerializableinterface for serialisation you are essentially taking control over the serialisation process. This allows you full control over handing versioning of serialised model classes. So you could even deserialise to a completely different class if you wanted to. Given the example above, serialisation into a Hashtable does not add value.As you have pointed out it decreases speed because you have to unbox the objects for each property request. If you have lots of property reads then this could really add up.
A hashtable scenario like this would be best used when you had to pass immutable types like string up several layers to a calling method. In that case leaving the properties boxed may be of some benefit. However because your
_mDatafield is protected and you are specifically exposing properties with types ofList<int>andstringthen I assume you do not intend to do this.If you wanted to continue your model you should at the very least cache your properties as strongly typed fields and covert type only once on first read.
Specially relating to versioning, if you expect the model to be updated in the future or have a definite need for compatibility allow for that now so that model versions can be identified. Previously when I have done this I have used a marker field (or XML attribute for XML serialisation) with a version number.
My suggestion would be to just serialise and deserialise to the types you need. If you version your class latter then allow for compatibility in you protected constructor and
GetObjectDatamethod.