Reputation: 1535
I have had a class like that:
[DataContract(Namespace = "blah")]
public class Item
{
[DataMember(Order = 0)]
public int Index { get; set; }
[DataMember(Order = 1)]
public Uri ItemSource { get; set; }
[DataMember(Order = 2)]
public Uri ErrorSource { get; set; }
}
And I have a lot of serialized copies (in files) of it (including some files on production), now I have the task to change this class to the following:
[DataContract(Namespace = "blah")]
public class Item
{
[DataMember(Order = 0)]
public int Index { get; set; }
[DataMember(Order = 1)]
public ItemSourcesCollection Sources { get; set; }
}
where ItemSourcesCollection is
[CollectionDataContract(ItemName = "ItemSourceItem", Namespace = "blah")]
public class ItemSourcesCollection : List<ItemSource> {}
where ItemSource is
[DataContract]
public class ItemSource
{
[DataMember]
public Uri SourcePath { get; set; }
[DataMember]
public ItemSourceType Type { get; set; }
}
where ItemSourceType is
[Serializable]
public enum ItemSourceType
{
Data,
Errors
}
The problem is a backward compatibility. Is it possible that old serialized items were deserialized correctly? What are the best practices/patterns of migrating the data contracts with backward compatibility?
Upvotes: 3
Views: 2219
Reputation: 3262
Yes it is possible with some reflection. I'm doing following thing for manually check previous versions during deserialization.
First use IExtensibleDataObject for a Item class:
[DataContract(Namespace = "blah")]
public class Item : IExtensibleDataObject
{
[DataMember(Order = 0)]
public int Index { get; set; }
[DataMember(Order = 1)]
public ItemSourcesCollection Sources { get; set; }
}
Now the tricky thing for deserialized method:
/// <summary>
/// The deserialized.
/// </summary>
/// <param name="context">
/// The streaming context.
/// </param>
[OnDeserialized]
private void Deserialized(StreamingContext context)
{
// reflection for backward compatibilty only
if (this.ExtensionData == null)
{
return;
}
IList members = this.CheckForExtensionDataMembers();
if (members == null)
{
return;
}
string value = this.GetExtensionDataMemberValue(members, "ItemSource");
// do something with value
value = this.GetExtensionDataMemberValue(members, "ErrorSource");
// do something with value
}
/// <summary>
/// The check for extension data members.
/// </summary>
/// <returns>
/// Thel list of extension data memebers.
/// </returns>
private IList CheckForExtensionDataMembers()
{
PropertyInfo membersProperty = typeof(ExtensionDataObject).GetProperty(
"Members", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
var members = (IList)membersProperty.GetValue(this.ExtensionData, null);
if (members == null || members.Count <= 0)
{
return null;
}
return members;
}
/// <summary>
/// The get extension data member value.
/// </summary>
/// <param name="members">
/// The members.
/// </param>
/// <param name="dataMemberName">
/// The data member name.
/// </param>
/// <returns>
/// Returns extension data member value.
/// </returns>
private string GetExtensionDataMemberValue(IList members, string dataMemberName)
{
string innerValue = null;
object member =
members.Cast<object>().FirstOrDefault(
m =>
((string)m.GetType().GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(m, null)).Equals(
dataMemberName, StringComparison.InvariantCultureIgnoreCase));
if (member != null)
{
PropertyInfo valueProperty = member.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
object value = valueProperty.GetValue(member, null);
PropertyInfo innerValueProperty = value.GetType().GetProperty("Value", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Public);
object tmp = innerValueProperty.GetValue(value, null);
var s = tmp as string;
if (s != null)
{
innerValue = s;
}
}
return innerValue;
}
All above stuff will be inside Item class.
Upvotes: 2