I'm currently using a BinaryFormater to serialize my ObjectGraph. Private members of type string serialize and deserialise well however private members of type DateTime do not deserialize back into their original datetime values - something is lost.
Can someone direct me on solving this problem? For the moment I'm casting my DateTime values to string before serialization and then using DateTime.Parse(...) to get the string representation back to type DateTime. It works but I'd prefer the correct way I must be doing something wrong!
The following fragment (C#) is how i'm marking my class for serialization:
[Serializable()]
public class SessionData
{
private DateTime _sessionDate;
Thanks for any direction on this.How about serialising the Ticks property of your DateTime object? Then you can instantiate a new DateTime object from that single Ticks value ...
(I haven't tested this ... just thinking aloud.)
Thanks for the suggestion. That would probably work as it would convert the DateTime to a Long data type, but I'm wondering why the problem with DateTime ?
I'm sure the framework is designed to serialise DateTime without the need for casting (pretty stupid if this is not the case!) so what am I missing ?
Well, DateTime is a Struct ... so it's closer to a class than it is to a base type (such as a string or integer). Hence, I don't think serialising will be quite so automatic ... to serialise a class, you generally have to do a bit of work.
But, as to that "bit of work", I was just wondering aloud whether Ticks might make your job easier.
And I could be completely wrong ... 'tis just a suggestion :)
0 comments:
Post a Comment