On Silverlight, I got a DataContract
like this:
[DataContract]
class Operation
{
[DataMember]
public string Name;
public readonly OperationManager Manager = new OperationManager();
}
I do not want to store or serialize Manager, which is why it's not a DataMember
. Manager is not-null when doing new Operation()
. But when I retrieve it from IsolatedStorageSettings
, I get a null Manager:
// operation.Manager is not null
var Settings = IsolatedStorageSettings.ApplicationSettings;
Settings["key"] = operation;
Settings.Save();
operation = (Operation)Settings["key"];
// operation.Manager is null
Is there a way to automatically re-initialize Manager to something new when it is being unserialized by IsolatedStorageSettings
? I tried to set it in constructor, but constructor is not called when going through IsolatedStorageSettings
.
The solution to initialize something coming from IsolatedStorageSettings
is either [OnDeserializing]
or [OnDeserialized]
(can't use .ctor(SerializationInfo info, StreamingContext context)
as not available on Silverlight).
[DataContract]
class Operation
{
[DataMember]
public string Name;
OperationManager _manager = new OperationManager();
public OperationManager Manager { get { return _manager; } }
[OnDeserializing]
#if WP7
internal
#endif
void OnDeserializing(StreamingContext c)
{
_manager = new OperationManager();
}
}
[OnDeserializing]
and [OnDeserialized]
must be internal on Windows Phone 7, and private on Visual Studio 2013. -_-