diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs index 6ca1e1f04..4b9a56939 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs @@ -140,5 +140,18 @@ namespace Google.Protobuf.WellKnownTypes var message = new TestWellKnownTypes { AnyField = new Any() }; Assert.AreEqual("{ \"anyField\": { \"@type\": \"\", \"@value\": \"\" } }", message.ToString()); } + + [Test] + public void IsWrongType() + { + var any = Any.Pack(SampleMessages.CreateFullTestAllTypes()); + Assert.False(any.Is(TestOneof.Descriptor)); + } + + public void IsRightType() + { + var any = Any.Pack(SampleMessages.CreateFullTestAllTypes()); + Assert.True(any.Is(TestAllTypes.Descriptor)); + } } } diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs index fca689dc0..b951c13a8 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs @@ -67,6 +67,17 @@ namespace Google.Protobuf.WellKnownTypes return lastSlash == -1 ? "" : typeUrl.Substring(lastSlash + 1); } + /// + /// Returns a bool indictating whether this Any message is of the target message type + /// + /// The descriptor of the message type + /// true if the type name matches the descriptor's full name or false otherwise + public bool Is(MessageDescriptor descriptor) + { + ProtoPreconditions.CheckNotNull(descriptor, nameof(descriptor)); + return GetTypeName(TypeUrl) == descriptor.FullName; + } + /// /// Unpacks the content of this Any message into the target message type, /// which must match the type URL within this Any message.