Deserializing xml to object with dictionary

前端 未结 5 863

This is my class structure:

class DataItem
{
   public string Id { get; set; }
   public string Type { get; set; }

   private Dictionary

        
5条回答
  •  一个人的身影
    2021-01-25 19:09

    The short answer: You cant

    This is because even though you can imagine the serialization scheme, .NET Serializer cannot guarantee that the keys re-serialized will have the same hash value.

    Long answer: You can, but it's a lot of work

    The solution for you is to either do manual serialization of the entire object, or to replace Dictionary with a simple Wrapper Property that wraps IDictionary, but exposes it as a list of pairs. (you can convert it later if you need it.)

    However: this leaves the same problem as above, you cannot gaurantee a re-serialized key will have the same hashcode as before it was serialized.

提交回复
热议问题