私はprotobuf-netシリアライザを使用していますが、今までは完璧に機能しています。いくつかのプライベート整数メンバをシリアライズする必要がありますが、シリアライズする前にバイト配列に集め、デシリアライズ時にバイト配列から抽出する必要がありますが、デシリアライズ時にはバイト配列のサイズが変更されます。デシリアライズ時のバイト配列のエラー
次のコードでは、整数を含むクラスを持つことによってこの問題を簡略化して示しています。直列化すると、長さ4のバイト配列に変換するゲッターからアクセスされます。セッターにはsize(2)の2倍のバイト配列が割り当てられているため、エラーが発生します。この種の変換はできませんか?
サイズ8のバイト配列の最後の4つのエントリには、実際にはシリアル化された値が含まれています。どうして?
PrivateValue
によって返される配列は[54, 100, 0, 0]
ですが、デシリアライズ時の配列は[0, 0, 0, 0, 54, 100, 0, 0]
です。
[ProtoBuf.ProtoContract]
class SerializeTest
{
public int Value { get; set; }
[ProtoBuf.ProtoMember(1)]
private byte[] PrivateValue
{
get
{
return new byte[4]
{
(byte)(Value),
(byte)(Value >> 8),
(byte)(Value >> 16),
(byte)(Value >> 24)
};
}
set
{
// For some reasone is the given byte array is always twice the size
// and all the values are in the last part og the array
this.Value = ((int)value[3] << 24) | ((int)value[2] << 16) | ((int)value[1] << 8) | value[0];
}
}
public override string ToString()
{
return this.Value.ToString();
}
}
class Program
{
static void Main(string[] args)
{
var a = new SerializeTest() { Value = 25654 };
using (var memStream = new MemoryStream())
{
// Serialize
ProtoBuf.Serializer.Serialize(memStream, a);
memStream.Position = 0;
// Deserialize
var data = ProtoBuf.Serializer.Deserialize<SerializeTest>(memStream);
// Writs 0 and not 25654
Console.WriteLine(data.Value);
}
}
}
'memStream.Position = 0;'の後に 'memStream.Length'とは何ですか? – SwDevMan81