当前位置: 首页>>代码示例>>C#>>正文


C# IMessage.HasField方法代码示例

本文整理汇总了C#中IMessage.HasField方法的典型用法代码示例。如果您正苦于以下问题:C# IMessage.HasField方法的具体用法?C# IMessage.HasField怎么用?C# IMessage.HasField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMessage的用法示例。


在下文中一共展示了IMessage.HasField方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AssertClearViaReflection

        /// <summary>
        /// Assert that all fields of the message are cleared, and that
        /// getting the fields returns their default values, using the reflection interface.
        /// </summary>
        public void AssertClearViaReflection(IMessage message)
        {
            // has_blah() should initially be false for all optional fields.
            Assert.IsFalse(message.HasField(f("optional_int32")));
            Assert.IsFalse(message.HasField(f("optional_int64")));
            Assert.IsFalse(message.HasField(f("optional_uint32")));
            Assert.IsFalse(message.HasField(f("optional_uint64")));
            Assert.IsFalse(message.HasField(f("optional_sint32")));
            Assert.IsFalse(message.HasField(f("optional_sint64")));
            Assert.IsFalse(message.HasField(f("optional_fixed32")));
            Assert.IsFalse(message.HasField(f("optional_fixed64")));
            Assert.IsFalse(message.HasField(f("optional_sfixed32")));
            Assert.IsFalse(message.HasField(f("optional_sfixed64")));
            Assert.IsFalse(message.HasField(f("optional_float")));
            Assert.IsFalse(message.HasField(f("optional_double")));
            Assert.IsFalse(message.HasField(f("optional_bool")));
            Assert.IsFalse(message.HasField(f("optional_string")));
            Assert.IsFalse(message.HasField(f("optional_bytes")));

            Assert.IsFalse(message.HasField(f("optionalgroup")));
            Assert.IsFalse(message.HasField(f("optional_nested_message")));
            Assert.IsFalse(message.HasField(f("optional_foreign_message")));
            Assert.IsFalse(message.HasField(f("optional_import_message")));

            Assert.IsFalse(message.HasField(f("optional_nested_enum")));
            Assert.IsFalse(message.HasField(f("optional_foreign_enum")));
            Assert.IsFalse(message.HasField(f("optional_import_enum")));

            Assert.IsFalse(message.HasField(f("optional_string_piece")));
            Assert.IsFalse(message.HasField(f("optional_cord")));

            // Optional fields without defaults are set to zero or something like it.
            Assert.AreEqual(0, message[f("optional_int32")]);
            Assert.AreEqual(0L, message[f("optional_int64")]);
            Assert.AreEqual(0U, message[f("optional_uint32")]);
            Assert.AreEqual(0UL, message[f("optional_uint64")]);
            Assert.AreEqual(0, message[f("optional_sint32")]);
            Assert.AreEqual(0L, message[f("optional_sint64")]);
            Assert.AreEqual(0U, message[f("optional_fixed32")]);
            Assert.AreEqual(0UL, message[f("optional_fixed64")]);
            Assert.AreEqual(0, message[f("optional_sfixed32")]);
            Assert.AreEqual(0L, message[f("optional_sfixed64")]);
            Assert.AreEqual(0F, message[f("optional_float")]);
            Assert.AreEqual(0D, message[f("optional_double")]);
            Assert.AreEqual(false, message[f("optional_bool")]);
            Assert.AreEqual("", message[f("optional_string")]);
            Assert.AreEqual(ByteString.Empty, message[f("optional_bytes")]);

            // Embedded messages should also be clear.
            Assert.IsFalse(((IMessage) message[f("optionalgroup")]).HasField(groupA));
            Assert.IsFalse(((IMessage) message[f("optional_nested_message")])
                               .HasField(nestedB));
            Assert.IsFalse(((IMessage) message[f("optional_foreign_message")])
                               .HasField(foreignC));
            Assert.IsFalse(((IMessage) message[f("optional_import_message")])
                               .HasField(importD));

            Assert.AreEqual(0, ((IMessage) message[f("optionalgroup")])[groupA]);
            Assert.AreEqual(0, ((IMessage) message[f("optional_nested_message")])[nestedB]);
            Assert.AreEqual(0, ((IMessage) message[f("optional_foreign_message")])[foreignC]);
            Assert.AreEqual(0, ((IMessage) message[f("optional_import_message")])[importD]);

            // Enums without defaults are set to the first value in the enum.
            Assert.AreEqual(nestedFoo, message[f("optional_nested_enum")]);
            Assert.AreEqual(foreignFoo, message[f("optional_foreign_enum")]);
            Assert.AreEqual(importFoo, message[f("optional_import_enum")]);

            Assert.AreEqual("", message[f("optional_string_piece")]);
            Assert.AreEqual("", message[f("optional_cord")]);

            // Repeated fields are empty.
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_int32")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_int64")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_uint32")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_uint64")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sint32")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sint64")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_fixed32")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_fixed64")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sfixed32")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sfixed64")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_float")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_double")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_bool")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_string")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_bytes")));

            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeatedgroup")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_nested_message")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_foreign_message")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_import_message")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_nested_enum")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_foreign_enum")));
            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_import_enum")));

            Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_string_piece")));
//.........这里部分代码省略.........
开发者ID:JuWell,项目名称:SmallNetGame,代码行数:101,代码来源:ReflectionTester.cs

示例2: FindMissingFields

        /// <summary>
        /// Recursive helper implementing FindMissingFields.
        /// </summary>
        private static void FindMissingFields(IMessage message, String prefix, List<String> results)
        {
            foreach (FieldDescriptor field in message.DescriptorForType.Fields)
            {
                if (field.IsRequired && !message.HasField(field))
                {
                    results.Add(prefix + field.Name);
                }
            }

            foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields)
            {
                FieldDescriptor field = entry.Key;
                object value = entry.Value;

                if (field.MappedType == MappedType.Message)
                {
                    if (field.IsRepeated)
                    {
                        int i = 0;
                        foreach (object element in (IEnumerable) value)
                        {
                            if (element is IMessage)
                            {
                                FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results);
                            }
                            else
                            {
                                results.Add(prefix + field.Name);
                            }
                        }
                    }
                    else
                    {
                        if (message.HasField(field))
                        {
                            if (value is IMessage)
                            {
                                FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results);
                            }
                            else
                            {
                                results.Add(prefix + field.Name);
                            }
                        }
                    }
                }
            }
        }
开发者ID:ZhouX1ang,项目名称:protobuf-csharp-port,代码行数:52,代码来源:UninitializedMessageException.cs

示例3: AssertAllFieldsSetViaReflection

        // -------------------------------------------------------------------

        /// <summary>
        /// Asserts that all fields of the specified message are set to the values
        /// assigned by SetAllFields, using the IMessage reflection interface.
        /// </summary>
        public void AssertAllFieldsSetViaReflection(IMessage message)
        {
            Assert.IsTrue(message.HasField(f("optional_int32")));
            Assert.IsTrue(message.HasField(f("optional_int64")));
            Assert.IsTrue(message.HasField(f("optional_uint32")));
            Assert.IsTrue(message.HasField(f("optional_uint64")));
            Assert.IsTrue(message.HasField(f("optional_sint32")));
            Assert.IsTrue(message.HasField(f("optional_sint64")));
            Assert.IsTrue(message.HasField(f("optional_fixed32")));
            Assert.IsTrue(message.HasField(f("optional_fixed64")));
            Assert.IsTrue(message.HasField(f("optional_sfixed32")));
            Assert.IsTrue(message.HasField(f("optional_sfixed64")));
            Assert.IsTrue(message.HasField(f("optional_float")));
            Assert.IsTrue(message.HasField(f("optional_double")));
            Assert.IsTrue(message.HasField(f("optional_bool")));
            Assert.IsTrue(message.HasField(f("optional_string")));
            Assert.IsTrue(message.HasField(f("optional_bytes")));

            Assert.IsTrue(message.HasField(f("optionalgroup")));
            Assert.IsTrue(message.HasField(f("optional_nested_message")));
            Assert.IsTrue(message.HasField(f("optional_foreign_message")));
            Assert.IsTrue(message.HasField(f("optional_import_message")));

            Assert.IsTrue(((IMessage) message[f("optionalgroup")]).HasField(groupA));
            Assert.IsTrue(((IMessage) message[f("optional_nested_message")]).HasField(nestedB));
            Assert.IsTrue(((IMessage) message[f("optional_foreign_message")]).HasField(foreignC));
            Assert.IsTrue(((IMessage) message[f("optional_import_message")]).HasField(importD));

            Assert.IsTrue(message.HasField(f("optional_nested_enum")));
            Assert.IsTrue(message.HasField(f("optional_foreign_enum")));
            Assert.IsTrue(message.HasField(f("optional_import_enum")));

            Assert.IsTrue(message.HasField(f("optional_string_piece")));
            Assert.IsTrue(message.HasField(f("optional_cord")));

            Assert.AreEqual(101, message[f("optional_int32")]);
            Assert.AreEqual(102L, message[f("optional_int64")]);
            Assert.AreEqual(103u, message[f("optional_uint32")]);
            Assert.AreEqual(104UL, message[f("optional_uint64")]);
            Assert.AreEqual(105, message[f("optional_sint32")]);
            Assert.AreEqual(106L, message[f("optional_sint64")]);
            Assert.AreEqual(107U, message[f("optional_fixed32")]);
            Assert.AreEqual(108UL, message[f("optional_fixed64")]);
            Assert.AreEqual(109, message[f("optional_sfixed32")]);
            Assert.AreEqual(110L, message[f("optional_sfixed64")]);
            Assert.AreEqual(111F, message[f("optional_float")]);
            Assert.AreEqual(112D, message[f("optional_double")]);
            Assert.AreEqual(true, message[f("optional_bool")]);
            Assert.AreEqual("115", message[f("optional_string")]);
            Assert.AreEqual(TestUtil.ToBytes("116"), message[f("optional_bytes")]);

            Assert.AreEqual(117, ((IMessage) message[f("optionalgroup")])[groupA]);
            Assert.AreEqual(118, ((IMessage) message[f("optional_nested_message")])[nestedB]);
            Assert.AreEqual(119, ((IMessage) message[f("optional_foreign_message")])[foreignC]);
            Assert.AreEqual(120, ((IMessage) message[f("optional_import_message")])[importD]);

            Assert.AreEqual(nestedBaz, message[f("optional_nested_enum")]);
            Assert.AreEqual(foreignBaz, message[f("optional_foreign_enum")]);
            Assert.AreEqual(importBaz, message[f("optional_import_enum")]);

            Assert.AreEqual("124", message[f("optional_string_piece")]);
            Assert.AreEqual("125", message[f("optional_cord")]);

            // -----------------------------------------------------------------

            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int32")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int64")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint32")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint64")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint32")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint64")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed32")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed64")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed32")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed64")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_float")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_double")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bool")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bytes")));

            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeatedgroup")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_message")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_message")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_message")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_enum")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_enum")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_enum")));

            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string_piece")));
            Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_cord")));

            Assert.AreEqual(201, message[f("repeated_int32"), 0]);
            Assert.AreEqual(202L, message[f("repeated_int64"), 0]);
//.........这里部分代码省略.........
开发者ID:JuWell,项目名称:SmallNetGame,代码行数:101,代码来源:ReflectionTester.cs


注:本文中的IMessage.HasField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。