Closed GoogleCodeExporter closed 9 years ago
The problem is that you are not providing an extension registry. You can still
access via the UnknownFields collection, or by providing an extension registry:
private static void Main()
{
TestMsgAdd tma = new TestMsgAdd.Builder().SetStr2("str2").Build();
TestMsg tm = new TestMsg.Builder().SetStr1("str1")
.SetExtension(TestMsgAdd.Data, tma).Build();
bool succ = tm.HasExtension(TestMsgAdd.Data); // TRUE
Assert.IsTrue(succ); // 15
Assert.AreEqual(15, tm.SerializedSize);
byte[] buff = tm.ToByteArray();
Assert.AreEqual(15, buff.Length);
TestMsg copy = TestMsg.ParseFrom(buff);
Assert.IsFalse(copy.HasExtension(TestMsgAdd.Data)); // Extension is unknown at time of deserialize
Assert.AreEqual(15, copy.SerializedSize);
// However, we can still access this via unkonwn fields:
UnknownField field;
Assert.IsTrue(copy.UnknownFields.FieldDictionary.TryGetValue(TestMsgAdd.Data.Number, out field));
var tmaCopy = TestMsgAdd.ParseFrom(field.LengthDelimitedList[0]);
Assert.AreEqual(tma, tmaCopy);
// Alternatively, when we deserialize we must provide an extension registry with the required extension
var registry = ExtensionRegistry.CreateInstance();
registry.Add(TestMsgAdd.Data);
copy = TestMsg.ParseFrom(buff, registry);
Assert.IsTrue(copy.HasExtension(TestMsgAdd.Data));
tmaCopy = copy.GetExtension(TestMsgAdd.Data);
Assert.AreEqual(tma, tmaCopy);
Console.WriteLine("Success!");
Console.ReadLine();
}
**NOTE: The UnknownFields collection is not available in the LITE_RUNTIME
optimization. You must use the extension registry in this case.
Original comment by Grig...@gmail.com
on 20 Jul 2013 at 5:21
Original issue reported on code.google.com by
anthony....@gmail.com
on 28 May 2013 at 7:11