If I have a class defined
[DataContract()]
class MyObject {
[DataMember()]
ImmutableList Strings { get; private set}
}
<
One way to do this is to use a proxy mutable list and use the OnSerializing and OnDeserialized hooks
[DataContract()]
class MyObject {
public ImmutableList Strings { get; private set}
[DataMember(Name="Strings")]
private List _Strings;
[OnSerializing()]
public void OnSerializing(StreamingContext ctxt){
_Strings = Strings.ToList();
}
[OnDeserialized()]
public void OnDeserialized(StreamingContext ctxt){
Strings = ImmutableList.Empty.AddRange(_Strings);
}
}
It's not super pretty but as Marc Gravell noted in his answer, DataContract serializer is broken with respects to immutable collections and there are no simple hooks to teach it how to behave without the above type of hack.
UPDATE
DataContract serializer is not broken. There is a way to hook surrogates in. See this separate answer showing an alternate technique.
https://stackoverflow.com/a/18957739/158285