我有一个有 2 个属性的类。一个属性是 HttpResponseMessage,我需要对其进行序列化。我在类上做了一个 [DataContract],在 HttpResponse 上做了一个 [DataMember]。这是课程:
[DataContract]
public class ResponseItem
{
[DataMember]
public HttpResponseMessage ResponseMessage { get; set; }
[DataMember]
public CacheInfoItem CacheInfo { get; set; }
}
问题是当我测试它然后我得到这个错误:
An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred
in mscorlib.dll but was not handled in user code
Additional information: Type 'System.Net.Http.StreamContent' cannot be serialized.
Consider marking it with the DataContractAttribute attribute, and marking all of its
members you want serialized with the DataMemberAttribute attribute. If the type is a
collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft
.NET Framework documentation for other supported types.
怎么了?
编辑:我有一个保存方法:
public static async Task SaveObjectAsync(Object toBeSaved, string fileName = null,
StorageLocation storageLocation = StorageLocation.Local, SerializerType serializerType =
SerializerType.DataContractJson)
{
Type type = toBeSaved.GetType();
fileName = getFileName(fileName, type, serializerType);
StorageFolder folder = getStorageFolder(storageLocation);
MemoryStream memoryStream = new MemoryStream();
Serializer serializer = getSerializer(serializerType, type);
serializer.WriteObject(memoryStream, toBeSaved);
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream fileStream = await file.OpenStreamForWriteAsync())
{
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
但看起来序列化程序无法处理 HttpResponseMessage