Is there a serializable generic Key/Value pair class in .NET?

前端 未结 10 1406
自闭症患者
自闭症患者 2020-11-29 01:59

I\'m looking for a key/value pair object that I can include in a web service.

I tried using .NET\'s System.Collections.Generic.KeyValuePair<> class, but it doe

10条回答
  •  天命终不由人
    2020-11-29 02:36

    Use the DataContractSerializer since it can handle the Key Value Pair.

        public static string GetXMLStringFromDataContract(object contractEntity)
        {
            using (System.IO.MemoryStream writer = new System.IO.MemoryStream())
            {
                var dataContractSerializer = new DataContractSerializer(contractEntity.GetType());
                dataContractSerializer.WriteObject(writer, contractEntity);
                writer.Position = 0;
                var streamReader = new System.IO.StreamReader(writer);
                return streamReader.ReadToEnd();
            }
        }
    

提交回复
热议问题