Add tests for extension accessor

pull/5936/head
Sydney Acksman 6 years ago
parent 0c80570a8b
commit 9163a6f1ad
  1. 122
      csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs
  2. 60
      csharp/src/Google.Protobuf.Test/SampleMessages.cs
  3. 11
      csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
  4. 2
      csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs

@ -31,10 +31,13 @@
#endregion #endregion
using Google.Protobuf.TestProtos; using Google.Protobuf.TestProtos;
using Proto2 = Google.Protobuf.TestProtos.Proto2;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf.Reflection namespace Google.Protobuf.Reflection
{ {
@ -44,36 +47,36 @@ namespace Google.Protobuf.Reflection
public void GetValue() public void GetValue()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); var message = SampleMessages.CreateFullTestAllTypes();
var fields = TestAllTypes.Descriptor.Fields; var fields = TestProtos.TestAllTypes.Descriptor.Fields;
Assert.AreEqual(message.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleBool, fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleBytes, fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleDouble, fields[TestProtos.TestAllTypes.SingleDoubleFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleFixed32, fields[TestAllTypes.SingleFixed32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleFixed32, fields[TestProtos.TestAllTypes.SingleFixed32FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleFixed64, fields[TestAllTypes.SingleFixed64FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleFixed64, fields[TestProtos.TestAllTypes.SingleFixed64FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleFloat, fields[TestAllTypes.SingleFloatFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleFloat, fields[TestProtos.TestAllTypes.SingleFloatFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleForeignEnum, fields[TestAllTypes.SingleForeignEnumFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleForeignEnum, fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleForeignMessage, fields[TestAllTypes.SingleForeignMessageFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleForeignMessage, fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleImportEnum, fields[TestAllTypes.SingleImportEnumFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleImportEnum, fields[TestProtos.TestAllTypes.SingleImportEnumFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleImportMessage, fields[TestAllTypes.SingleImportMessageFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleImportMessage, fields[TestProtos.TestAllTypes.SingleImportMessageFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleInt32, fields[TestAllTypes.SingleInt32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleInt32, fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleInt64, fields[TestAllTypes.SingleInt64FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleInt64, fields[TestProtos.TestAllTypes.SingleInt64FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleNestedEnum, fields[TestAllTypes.SingleNestedEnumFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleNestedEnum, fields[TestProtos.TestAllTypes.SingleNestedEnumFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleNestedMessage, fields[TestAllTypes.SingleNestedMessageFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleNestedMessage, fields[TestProtos.TestAllTypes.SingleNestedMessageFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SinglePublicImportMessage, fields[TestAllTypes.SinglePublicImportMessageFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SinglePublicImportMessage, fields[TestProtos.TestAllTypes.SinglePublicImportMessageFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleSint32, fields[TestAllTypes.SingleSint32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleSint32, fields[TestProtos.TestAllTypes.SingleSint32FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleSint64, fields[TestAllTypes.SingleSint64FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleSint64, fields[TestProtos.TestAllTypes.SingleSint64FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleString, fields[TestAllTypes.SingleStringFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleString, fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleSfixed32, fields[TestAllTypes.SingleSfixed32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleSfixed32, fields[TestProtos.TestAllTypes.SingleSfixed32FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleSfixed64, fields[TestAllTypes.SingleSfixed64FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleSfixed64, fields[TestProtos.TestAllTypes.SingleSfixed64FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleUint32, fields[TestAllTypes.SingleUint32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleUint32, fields[TestProtos.TestAllTypes.SingleUint32FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.SingleUint64, fields[TestAllTypes.SingleUint64FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.SingleUint64, fields[TestProtos.TestAllTypes.SingleUint64FieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.OneofBytes, fields[TestAllTypes.OneofBytesFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.OneofBytes, fields[TestProtos.TestAllTypes.OneofBytesFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.OneofString, fields[TestAllTypes.OneofStringFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.OneofString, fields[TestProtos.TestAllTypes.OneofStringFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.OneofNestedMessage, fields[TestAllTypes.OneofNestedMessageFieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.OneofNestedMessage, fields[TestProtos.TestAllTypes.OneofNestedMessageFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(message.OneofUint32, fields[TestAllTypes.OneofUint32FieldNumber].Accessor.GetValue(message)); Assert.AreEqual(message.OneofUint32, fields[TestProtos.TestAllTypes.OneofUint32FieldNumber].Accessor.GetValue(message));
// Just one example for repeated fields - they're all just returning the list // Just one example for repeated fields - they're all just returning the list
var list = (IList) fields[TestAllTypes.RepeatedInt32FieldNumber].Accessor.GetValue(message); var list = (IList) fields[TestProtos.TestAllTypes.RepeatedInt32FieldNumber].Accessor.GetValue(message);
Assert.AreEqual(message.RepeatedInt32, list); Assert.AreEqual(message.RepeatedInt32, list);
Assert.AreEqual(message.RepeatedInt32[0], list[0]); // Just in case there was any doubt... Assert.AreEqual(message.RepeatedInt32[0], list[0]); // Just in case there was any doubt...
@ -85,18 +88,27 @@ namespace Google.Protobuf.Reflection
Assert.AreEqual("value1", dictionary["key1"]); Assert.AreEqual("value1", dictionary["key1"]);
} }
[Test]
public void GetExtensionValue()
{
var message = SampleMessages.CreateFullTestAllExtensions();
// test that the reflector works, since the reflector just runs through IExtendableMessage
Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message));
}
[Test] [Test]
public void Clear() public void Clear()
{ {
var message = SampleMessages.CreateFullTestAllTypes(); var message = SampleMessages.CreateFullTestAllTypes();
var fields = TestAllTypes.Descriptor.Fields; var fields = TestProtos.TestAllTypes.Descriptor.Fields;
fields[TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message);
fields[TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message);
fields[TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message);
fields[TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message);
fields[TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message);
fields[TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message);
fields[TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message); fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message);
var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes()) var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes())
{ {
@ -123,14 +135,14 @@ namespace Google.Protobuf.Reflection
{ {
// Just a sample (primitives, messages, enums, strings, byte strings) // Just a sample (primitives, messages, enums, strings, byte strings)
var message = SampleMessages.CreateFullTestAllTypes(); var message = SampleMessages.CreateFullTestAllTypes();
var fields = TestAllTypes.Descriptor.Fields; var fields = TestProtos.TestAllTypes.Descriptor.Fields;
fields[TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, false); fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, false);
fields[TestAllTypes.SingleInt32FieldNumber].Accessor.SetValue(message, 500); fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.SetValue(message, 500);
fields[TestAllTypes.SingleStringFieldNumber].Accessor.SetValue(message, "It's a string"); fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.SetValue(message, "It's a string");
fields[TestAllTypes.SingleBytesFieldNumber].Accessor.SetValue(message, ByteString.CopyFrom(99, 98, 97)); fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.SetValue(message, ByteString.CopyFrom(99, 98, 97));
fields[TestAllTypes.SingleForeignEnumFieldNumber].Accessor.SetValue(message, ForeignEnum.ForeignFoo); fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.SetValue(message, ForeignEnum.ForeignFoo);
fields[TestAllTypes.SingleForeignMessageFieldNumber].Accessor.SetValue(message, new ForeignMessage { C = 12345 }); fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.SetValue(message, new ForeignMessage { C = 12345 });
fields[TestAllTypes.SingleDoubleFieldNumber].Accessor.SetValue(message, 20150701.5); fields[TestProtos.TestAllTypes.SingleDoubleFieldNumber].Accessor.SetValue(message, 20150701.5);
var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes()) var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes())
{ {
@ -151,7 +163,7 @@ namespace Google.Protobuf.Reflection
{ {
IMessage message = SampleMessages.CreateFullTestAllTypes(); IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, "This isn't a bool")); Assert.Throws<InvalidCastException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, "This isn't a bool"));
} }
[Test] [Test]
@ -167,7 +179,7 @@ namespace Google.Protobuf.Reflection
{ {
IMessage message = SampleMessages.CreateFullTestAllTypes(); IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
Assert.Throws<InvalidOperationException>(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].Accessor.SetValue(message, new double[10])); Assert.Throws<InvalidOperationException>(() => fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.SetValue(message, new double[10]));
} }
[Test] [Test]
@ -175,42 +187,42 @@ namespace Google.Protobuf.Reflection
{ {
IMessage message = SampleMessages.CreateFullTestAllTypes(); IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(new TestMap())); Assert.Throws<InvalidCastException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(new TestMap()));
} }
[Test] [Test]
public void Oneof() public void Oneof()
{ {
var message = new TestAllTypes(); var message = new TestAllTypes();
var descriptor = TestAllTypes.Descriptor; var descriptor = TestProtos.TestAllTypes.Descriptor;
Assert.AreEqual(1, descriptor.Oneofs.Count); Assert.AreEqual(1, descriptor.Oneofs.Count);
var oneof = descriptor.Oneofs[0]; var oneof = descriptor.Oneofs[0];
Assert.AreEqual("oneof_field", oneof.Name); Assert.AreEqual("oneof_field", oneof.Name);
Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message)); Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofString = "foo"; message.OneofString = "foo";
Assert.AreSame(descriptor.Fields[TestAllTypes.OneofStringFieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message)); Assert.AreSame(descriptor.Fields[TestProtos.TestAllTypes.OneofStringFieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofUint32 = 10; message.OneofUint32 = 10;
Assert.AreSame(descriptor.Fields[TestAllTypes.OneofUint32FieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message)); Assert.AreSame(descriptor.Fields[TestProtos.TestAllTypes.OneofUint32FieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message));
oneof.Accessor.Clear(message); oneof.Accessor.Clear(message);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
} }
[Test] [Test]
public void FieldDescriptor_ByName() public void FieldDescriptor_ByName()
{ {
var descriptor = TestAllTypes.Descriptor; var descriptor = TestProtos.TestAllTypes.Descriptor;
Assert.AreSame( Assert.AreSame(
descriptor.Fields[TestAllTypes.SingleBoolFieldNumber], descriptor.Fields[TestProtos.TestAllTypes.SingleBoolFieldNumber],
descriptor.Fields["single_bool"]); descriptor.Fields["single_bool"]);
} }
[Test] [Test]
public void FieldDescriptor_NotFound() public void FieldDescriptor_NotFound()
{ {
var descriptor = TestAllTypes.Descriptor; var descriptor = TestProtos.TestAllTypes.Descriptor;
Assert.Throws<KeyNotFoundException>(() => descriptor.Fields[999999].ToString()); Assert.Throws<KeyNotFoundException>(() => descriptor.Fields[999999].ToString());
Assert.Throws<KeyNotFoundException>(() => descriptor.Fields["not found"].ToString()); Assert.Throws<KeyNotFoundException>(() => descriptor.Fields["not found"].ToString());
} }

@ -32,7 +32,9 @@
using System; using System;
using Google.Protobuf.TestProtos; using Google.Protobuf.TestProtos;
using Proto2 = Google.Protobuf.TestProtos.Proto2; using Proto2 = Google.Protobuf.TestProtos.Proto2;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf namespace Google.Protobuf
{ {
@ -61,7 +63,7 @@ namespace Google.Protobuf
SingleImportMessage = new ImportMessage { D = 20 }, SingleImportMessage = new ImportMessage { D = 20 },
SingleInt32 = 100, SingleInt32 = 100,
SingleInt64 = 3210987654321, SingleInt64 = 3210987654321,
SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, SingleNestedEnum = TestProtos.TestAllTypes.Types.NestedEnum.Foo,
SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
SinglePublicImportMessage = new PublicImportMessage { E = 54 }, SinglePublicImportMessage = new PublicImportMessage { E = 54 },
SingleSfixed32 = -123, SingleSfixed32 = -123,
@ -83,7 +85,7 @@ namespace Google.Protobuf
RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } }, RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
RepeatedInt32 = { 100, 200 }, RepeatedInt32 = { 100, 200 },
RepeatedInt64 = { 3210987654321, Int64.MaxValue }, RepeatedInt64 = { 3210987654321, Int64.MaxValue },
RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg }, RepeatedNestedEnum = { TestProtos.TestAllTypes.Types.NestedEnum.Foo, TestProtos.TestAllTypes.Types.NestedEnum.Neg },
RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } }, RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
RepeatedSfixed32 = { -123, 123 }, RepeatedSfixed32 = { -123, 123 },
@ -149,5 +151,57 @@ namespace Google.Protobuf
OneofString = "Oneof string" OneofString = "Oneof string"
}; };
} }
public static Proto2.TestAllExtensions CreateFullTestAllExtensions()
{
var message = new Proto2.TestAllExtensions();
message.SetExtension(OptionalBoolExtension, true);
message.SetExtension(OptionalBytesExtension, ByteString.CopyFrom(1, 2, 3, 4));
message.SetExtension(OptionalDoubleExtension, 23.5);
message.SetExtension(OptionalFixed32Extension, 23u);
message.SetExtension(OptionalFixed64Extension, 1234567890123u);
message.SetExtension(OptionalFloatExtension, 12.25f);
message.SetExtension(OptionalForeignEnumExtension, Proto2.ForeignEnum.ForeignBar);
message.SetExtension(OptionalForeignMessageExtension, new Proto2.ForeignMessage { C = 10 });
message.SetExtension(OptionalImportEnumExtension, Proto2.ImportEnum.ImportBaz);
message.SetExtension(OptionalImportMessageExtension, new Proto2.ImportMessage { D = 20 });
message.SetExtension(OptionalInt32Extension, 100);
message.SetExtension(OptionalInt64Extension, 3210987654321);
message.SetExtension(OptionalNestedEnumExtension, Proto2.TestAllTypes.Types.NestedEnum.Foo);
message.SetExtension(OptionalNestedMessageExtension, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 });
message.SetExtension(OptionalPublicImportMessageExtension, new Proto2.PublicImportMessage { E = 54 });
message.SetExtension(OptionalSfixed32Extension, -123);
message.SetExtension(OptionalSfixed64Extension, -12345678901234);
message.SetExtension(OptionalSint32Extension, -456);
message.SetExtension(OptionalSint64Extension, -12345678901235);
message.SetExtension(OptionalStringExtension, "test");
message.SetExtension(OptionalUint32Extension, UInt32.MaxValue);
message.SetExtension(OptionalUint64Extension, UInt64.MaxValue);
message.SetExtension(OptionalGroupExtension, new Proto2.OptionalGroup_extension { A = 10 });
message.GetOrRegisterExtension(RepeatedBoolExtension).AddRange(new[] { true, false });
message.GetOrRegisterExtension(RepeatedBytesExtension).AddRange(new[] { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) });
message.GetOrRegisterExtension(RepeatedDoubleExtension).AddRange(new[] { -12.25, 23.5 });
message.GetOrRegisterExtension(RepeatedFixed32Extension).AddRange(new[] { UInt32.MaxValue, 23u });
message.GetOrRegisterExtension(RepeatedFixed64Extension).AddRange(new[] { UInt64.MaxValue, 1234567890123ul });
message.GetOrRegisterExtension(RepeatedFloatExtension).AddRange(new[] { 100f, 12.25f });
message.GetOrRegisterExtension(RepeatedForeignEnumExtension).AddRange(new[] { Proto2.ForeignEnum.ForeignFoo, Proto2.ForeignEnum.ForeignBar });
message.GetOrRegisterExtension(RepeatedForeignMessageExtension).AddRange(new[] { new Proto2.ForeignMessage(), new Proto2.ForeignMessage { C = 10 } });
message.GetOrRegisterExtension(RepeatedImportEnumExtension).AddRange(new[] { Proto2.ImportEnum.ImportBaz, Proto2.ImportEnum.ImportFoo });
message.GetOrRegisterExtension(RepeatedImportMessageExtension).AddRange(new[] { new Proto2.ImportMessage { D = 20 }, new Proto2.ImportMessage { D = 25 } });
message.GetOrRegisterExtension(RepeatedInt32Extension).AddRange(new[] { 100, 200 });
message.GetOrRegisterExtension(RepeatedInt64Extension).AddRange(new[] { 3210987654321, Int64.MaxValue });
message.GetOrRegisterExtension(RepeatedNestedEnumExtension).AddRange(new[] { Proto2.TestAllTypes.Types.NestedEnum.Foo, Proto2.TestAllTypes.Types.NestedEnum.Neg });
message.GetOrRegisterExtension(RepeatedNestedMessageExtension).AddRange(new[] { new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 }, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 10 } });
message.GetOrRegisterExtension(RepeatedSfixed32Extension).AddRange(new[] { -123, 123 });
message.GetOrRegisterExtension(RepeatedSfixed64Extension).AddRange(new[] { -12345678901234, 12345678901234 });
message.GetOrRegisterExtension(RepeatedSint32Extension).AddRange(new[] { -456, 100 });
message.GetOrRegisterExtension(RepeatedSint64Extension).AddRange(new[] { -12345678901235, 123 });
message.GetOrRegisterExtension(RepeatedStringExtension).AddRange(new[] { "foo", "bar" });
message.GetOrRegisterExtension(RepeatedUint32Extension).AddRange(new[] { UInt32.MaxValue, UInt32.MinValue });
message.GetOrRegisterExtension(RepeatedUint64Extension).AddRange(new[] { UInt64.MaxValue, UInt32.MinValue });
message.GetOrRegisterExtension(RepeatedGroupExtension).AddRange(new[] { new Proto2.RepeatedGroup_extension { A = 10 }, new Proto2.RepeatedGroup_extension { A = 20 } });
message.SetExtension(OneofStringExtension, "Oneof string");
return message;
}
} }
} }

@ -380,7 +380,12 @@ namespace Google.Protobuf.Reflection
} }
private IFieldAccessor CreateAccessor() private IFieldAccessor CreateAccessor()
{ {
if (Extension != null)
{
return new ExtensionAccessor(this);
}
// If we're given no property name, that's because we really don't want an accessor. // If we're given no property name, that's because we really don't want an accessor.
// This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically. // This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically.
// TODO: Support dynamic messages. // TODO: Support dynamic messages.
@ -389,10 +394,6 @@ namespace Google.Protobuf.Reflection
return null; return null;
} }
if (Extension != null)
{
return new ExtensionAccessor(this);
}
var property = ContainingType.ClrType.GetProperty(propertyName); var property = ContainingType.ClrType.GetProperty(propertyName);
if (property == null) if (property == null)
{ {

@ -123,7 +123,7 @@ namespace Google.Protobuf.Reflection
/// the type that declares the method, and the second argument to the first parameter type of the method. /// the type that declares the method, and the second argument to the first parameter type of the method.
/// </summary> /// </summary>
internal static IExtensionReflectionHelper CreateExtensionHelper(Extension extension) => internal static IExtensionReflectionHelper CreateExtensionHelper(Extension extension) =>
(IExtensionReflectionHelper)Activator.CreateInstance(typeof(ExtensionReflectionHelper<,>).MakeGenericType(extension.TargetType, extension.GetType().GenericTypeArguments[1])); (IExtensionReflectionHelper)Activator.CreateInstance(typeof(ExtensionReflectionHelper<,>).MakeGenericType(extension.TargetType, extension.GetType().GenericTypeArguments[1]), extension);
/// <summary> /// <summary>
/// Creates a reflection helper for the given type arguments. Currently these are created on demand /// Creates a reflection helper for the given type arguments. Currently these are created on demand

Loading…
Cancel
Save