aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Tattermusch <jtattermusch@users.noreply.github.com>2015-10-30 07:33:39 -0700
committerJan Tattermusch <jtattermusch@users.noreply.github.com>2015-10-30 07:33:39 -0700
commit46f8a79826dbd6d9c7968baa4939d7e1b6e2b47c (patch)
tree0eef3fbf64b377e72e1dd3b5ed891903e04909c6
parenta74e912a8be1274fd561db5e8133937d4e9c4a2b (diff)
parentf5a0a7feeb7dcde7f5eddd4548cbf08e9e3ccd57 (diff)
downloadprotobuf-46f8a79826dbd6d9c7968baa4939d7e1b6e2b47c.tar.gz
protobuf-46f8a79826dbd6d9c7968baa4939d7e1b6e2b47c.tar.bz2
protobuf-46f8a79826dbd6d9c7968baa4939d7e1b6e2b47c.zip
Merge pull request #905 from jskeet/wrapper-clear
Fix clearing wrapper type fields with reflection.
-rw-r--r--csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs10
-rw-r--r--csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs2
-rw-r--r--csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs2
3 files changed, 12 insertions, 2 deletions
diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
index fbc0ff07..c87ceb2f 100644
--- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
+++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
@@ -345,5 +345,15 @@ namespace Google.Protobuf.WellKnownTypes
var message = TestWellKnownTypes.Parser.ParseFrom(stream);
Assert.AreEqual(6, message.Int32Field);
}
+
+ [Test]
+ public void ClearWithReflection()
+ {
+ // String and Bytes are the tricky ones here, as the CLR type of the property
+ // is the same between the wrapper and non-wrapper types.
+ var message = new TestWellKnownTypes { StringField = "foo" };
+ TestWellKnownTypes.Descriptor.Fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.Clear(message);
+ Assert.IsNull(message.StringField);
+ }
}
}
diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
index 619a6ac8..3d6cc59f 100644
--- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
+++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
@@ -197,7 +197,7 @@ namespace Google.Protobuf.Reflection
}
/// <summary>
- /// Returns the type of the field.
+ /// Returns the type of the field.
/// </summary>
public FieldType FieldType
{
diff --git a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs
index de92fbc1..bbac2173 100644
--- a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs
+++ b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs
@@ -61,7 +61,7 @@ namespace Google.Protobuf.Reflection
// TODO: Validate that this is a reasonable single field? (Should be a value type, a message type, or string/ByteString.)
object defaultValue =
- typeof(IMessage).IsAssignableFrom(clrType) ? null
+ descriptor.FieldType == FieldType.Message ? null
: clrType == typeof(string) ? ""
: clrType == typeof(ByteString) ? ByteString.Empty
: Activator.CreateInstance(clrType);