如何序列化一个类的Stream (或者更准确地说是Stream派生的)数据成员?
假设我们有一个不能归属的第三方类:
public class Fubar
{
public Fubar() { ... }
public string Label { get; set; }
public int DataType { get; set; }
public Stream Data { get; set; } // Where it's always actually MemoryStream
};我正在尝试使用protobuf-net来序列化这个类。解决我遇到的异常和各种SO问题:
RuntimeTypeModel.Default.Add(typeof(Stream), true)
.AddSubType(1, typeof(MemoryStream));
RuntimeTypeModel.Default.Add(typeof(Fubar), false)
.Add(1, "Label")
.Add(2, "DataType")
.Add(3, "Data");
using (MemoryStream ms = new MemoryStream())
{
Fubar f1 = new Fubar();
/* f1 initialized */
// Serialize f1
Serializer.SerializeWithLengthPrefix<Message>(ms, f1, PrefixStyle.Base128);
// Now let's de-serialize
ms.Position = 0;
Fubar f2 = Serializer.DeserializeWithLengthPrefix<Fubar>(ms, PrefixStyle.Base128);
}上面的运行没有任何错误。f2中的标签和DataType是正确的,但数据变量只是一个空流。调试代码时,我看到内存流大约是29字节(而f1中的数据流超过77KiB)。
我觉得我好像错过了一些相当微不足道的东西,但似乎就是想不出它会是什么。我假设确实可以序列化一个流数据成员。我是否还需要以某种方式指定Stream或MemoryStream类型的数据属性?
发布于 2013-06-27 04:53:44
Stream是一个非常复杂的东西,没有内置的序列化机制。您的代码将其配置为没有有趣成员的类型,这就是它返回为空的原因。
对于这个场景,我可能会创建一个代理,并使用以下命令设置它:
RuntimeTypeModel.Default.Add(typeof(Fubar), false)
.SetSurrogate(typeof(FubarSurrogate));其中:
[ProtoContract]
public class FubarSurrogate
{
[ProtoMember(1)]
public string Label { get; set; }
[ProtoMember(2)]
public int DataType { get; set; }
[ProtoMember(3)]
public byte[] Data { get; set; }
public static explicit operator Fubar(FubarSurrogate value)
{
if(value == null) return null;
return new Fubar {
Label = value.Label,
DataType = value.DataType,
Data = value.Data == null ? null : new MemoryStream(value.Data)
};
}
public static explicit operator FubarSurrogate(Fubar value)
{
if (value == null) return null;
return new FubarSurrogate
{
Label = value.Label,
DataType = value.DataType,
Data = value.Data == null ?
null : ((MemoryStream)value.Data).ToArray()
};
}
}发布于 2013-06-27 11:47:10
不是为了让马克抓自己的手。但是,如果其他人想要为Stream创建一个代理,我从答案中改编了Marc的代理示例:
[ProtoContract]
public class StreamSurrogate
{
[ProtoMember(1)]
public byte[] Data { get; set; }
public static explicit operator Stream(StreamSurrogate value)
{
if (value == null)
{
return null;
}
return new MemoryStream(value.Data);
}
public static explicit operator StreamSurrogate(Stream value)
{
if (value == null)
{
return null;
}
if (value is MemoryStream)
{
return new StreamSurrogate { Data = ((MemoryStream)value).ToArray() };
}
else
{
// Probably a better way to do this...
StreamSurrogate ss = new StreamSurrogate();
ss.Data = new byte[value.Length];
value.Read(ss.Data, 0, (int)value.Length);
return ss;
}
}
}然后对于RuntimeTypeModel:
MetaType mt2 = RuntimeTypeModel.Default.Add(typeof(Stream), true);
mt2.AddSubType(1, typeof(MemoryStream));
mt2.SetSurrogate(typeof(StreamSurrogate));在这种情况下,我的示例中的f2似乎是完全正确的!
是的,尝试序列化Stream有很多潜在的问题--也许对我来说,更明智的做法是只在MemoryStream子类型上设置代理,因为我知道我的特定情况总是使用MemoryStreams存储数据。然而,我在Stream上注册代理的思路如下:
Stream
https://stackoverflow.com/questions/17327277
复制相似问题