Beispiel #1
0
 /// <summary>
 /// Reads an enum field value from the stream. The caller is responsible
 /// for converting the numeric value to an actual enum.
 /// </summary>
 public bool ReadEnum(ref IEnumLite value, out object unknown, IEnumLiteMap mapping)
 {
     tokenizer.Consume(":");
     if (tokenizer.LookingAtInteger())
     {
         int rawValue = tokenizer.ConsumeInt32();
         value = mapping.FindValueByNumber(rawValue);
         if (value != null)
         {
             unknown = null;
             return(true);
         }
         unknown = rawValue;
     }
     else
     {
         string rawName = tokenizer.ConsumeIdentifier();
         value = mapping.FindValueByName(rawName);
         if (value != null)
         {
             unknown = null;
             return(true);
         }
         unknown = rawName;
     }
     return(false);
 }
        public object SingularFromReflectionType(object value)
        {
            switch (Descriptor.MappedType)
            {
            case MappedType.Message:
                if (value is TExtensionType)
                {
                    return(value);
                }
                else
                {
                    // It seems the copy of the embedded message stored inside the
                    // extended message is not of the exact type the user was
                    // expecting.  This can happen if a user defines a
                    // GeneratedExtension manually and gives it a different type.
                    // This should not happen in normal use.  But, to be nice, we'll
                    // copy the message to whatever type the caller was expecting.
                    return(MessageDefaultInstance.WeakCreateBuilderForType()
                           .WeakMergeFrom((IMessageLite)value).WeakBuild());
                }

            case MappedType.Enum:
                // Just return a boxed int - that can be unboxed to the enum
                IEnumLite enumValue = (IEnumLite)value;
                return(enumValue.Number);

            default:
                return(value);
            }
        }
Beispiel #3
0
        void ICodedInputStream.ReadEnumArray(uint fieldTag, string fieldName, ICollection <IEnumLite> list,
                                             out ICollection <object> unknown, IEnumLiteMap mapping)
        {
            unknown = null;
            List <object> array = new List <object>();

            if (ReadEnumArray(fieldName, array))
            {
                foreach (object rawValue in array)
                {
                    IEnumLite item = null;
                    if (rawValue is int)
                    {
                        item = mapping.FindValueByNumber((int)rawValue);
                    }
                    else if (rawValue is string)
                    {
                        item = mapping.FindValueByName((string)rawValue);
                    }

                    if (item != null)
                    {
                        list.Add(item);
                    }
                    else
                    {
                        if (unknown == null)
                        {
                            unknown = new List <object>();
                        }
                        unknown.Add(rawValue);
                    }
                }
            }
        }
        public bool ReadEnum(ref IEnumLite value, out object unknown, IEnumLiteMap mapping)
        {
            int num = (int)this.ReadRawVarint32();

            value = mapping.FindValueByNumber(num);
            if (value != null)
            {
                unknown = null;
                return(true);
            }
            unknown = num;
            return(false);
        }
        public void ReadEnumArray(uint fieldTag, string fieldName, ICollection <IEnumLite> list, out ICollection <object> unknown, IEnumLiteMap mapping)
        {
            unknown = null;
            IEnumLite enumLite = null;

            WireFormat.WireType tagWireType = WireFormat.GetTagWireType(fieldTag);
            if (tagWireType == WireFormat.WireType.LengthDelimited)
            {
                int byteLimit = (int)(this.ReadRawVarint32() & 2147483647u);
                int oldLimit  = this.PushLimit(byteLimit);
                while (!this.ReachedLimit)
                {
                    object obj;
                    if (this.ReadEnum(ref enumLite, out obj, mapping))
                    {
                        list.Add(enumLite);
                    }
                    else
                    {
                        if (unknown == null)
                        {
                            unknown = new List <object>();
                        }
                        unknown.Add(obj);
                    }
                }
                this.PopLimit(oldLimit);
                return;
            }
            do
            {
                object obj;
                if (this.ReadEnum(ref enumLite, out obj, mapping))
                {
                    list.Add(enumLite);
                }
                else
                {
                    if (unknown == null)
                    {
                        unknown = new List <object>();
                    }
                    unknown.Add(obj);
                }
            }while (this.ContinueArray(fieldTag));
        }
Beispiel #6
0
 bool ICodedInputStream.ReadEnum(ref IEnumLite value, out object unknown, IEnumLiteMap mapping)
 {
     value   = null;
     unknown = null;
     if (ReadEnum(ref unknown))
     {
         if (unknown is int)
         {
             value = mapping.FindValueByNumber((int)unknown);
         }
         else if (unknown is string)
         {
             value = mapping.FindValueByName((string)unknown);
         }
         return(value != null);
     }
     return(false);
 }
        public object SingularFromReflectionType(object value)
        {
            switch (this.Descriptor.MappedType)
            {
            case MappedType.Message:
                if (value is TExtensionType)
                {
                    return(value);
                }
                return(this.MessageDefaultInstance.WeakCreateBuilderForType().WeakMergeFrom((IMessageLite)value).WeakBuild());

            case MappedType.Enum:
            {
                IEnumLite enumLite = (IEnumLite)value;
                return(enumLite.Number);
            }

            default:
                return(value);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Verifies that the given object is of the correct type to be a valid
        /// value for the given field.
        /// </summary>
        /// <remarks>
        /// For repeated fields, this checks if the object is of the right
        /// element type, not whether it's a list.
        /// </remarks>
        /// <exception cref="ArgumentException">The value is not of the right type.</exception>
        /// <exception cref="ArgumentNullException">The value is null.</exception>
        private static void VerifyType(IFieldDescriptorLite field, object value)
        {
            ThrowHelper.ThrowIfNull(value, "value");
            bool isValid = false;

            switch (field.MappedType)
            {
            case MappedType.Int32:       isValid = value is int;    break;

            case MappedType.Int64:       isValid = value is long;   break;

            case MappedType.UInt32:      isValid = value is uint;   break;

            case MappedType.UInt64:      isValid = value is ulong;  break;

            case MappedType.Single:      isValid = value is float;  break;

            case MappedType.Double:      isValid = value is double; break;

            case MappedType.Boolean:     isValid = value is bool;   break;

            case MappedType.String:      isValid = value is string; break;

            case MappedType.ByteString:  isValid = value is ByteString; break;

            case MappedType.Enum:
                IEnumLite enumValue = value as IEnumLite;
                isValid = enumValue != null && field.EnumType.IsValidValue(enumValue);
                break;

            case MappedType.Message:
                IMessageLite messageValue = value as IMessageLite;
                isValid = messageValue != null;
#if !LITE
                if (isValid && messageValue is IMessage && field is FieldDescriptor)
                {
                    isValid = ((IMessage)messageValue).DescriptorForType == ((FieldDescriptor)field).MessageType;
                }
#endif
                break;
            }

            if (!isValid)
            {
                // When chaining calls to SetField(), it can be hard to tell from
                // the stack trace which exact call failed, since the whole chain is
                // considered one line of code.  So, let's make sure to include the
                // field name and other useful info in the exception.
                string message = "Wrong object type used with protocol message reflection.";
#if !LITE
                Google.ProtocolBuffers.Descriptors.FieldDescriptor fieldinfo = field as Google.ProtocolBuffers.Descriptors.FieldDescriptor;
                if (fieldinfo != null)
                {
                    message += "Message type \"" + fieldinfo.ContainingType.FullName;
                    message += "\", field \"" + (fieldinfo.IsExtension ? fieldinfo.FullName : fieldinfo.Name);
                    message += "\", value was type \"" + value.GetType().Name + "\".";
                }
#endif
                throw new ArgumentException(message);
            }
        }
        protected override bool ParseUnknownField(ICodedInputStream input,
                                                  ExtensionRegistry extensionRegistry, uint tag, string fieldName)
        {
            FieldSet extensions = MessageBeingBuilt.Extensions;

            WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
            int fieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];

            if (extension == null) //unknown field
            {
                return(input.SkipField());
            }

            IFieldDescriptorLite field = extension.Descriptor;


            // Unknown field or wrong wire type. Skip.
            if (field == null)
            {
                return(input.SkipField());
            }
            WireFormat.WireType expectedType = field.IsPacked
                                                   ? WireFormat.WireType.LengthDelimited
                                                   : WireFormat.GetWireType(field.FieldType);
            if (wireType != expectedType)
            {
                expectedType = WireFormat.GetWireType(field.FieldType);
                if (wireType == expectedType)
                {
                    //Allowed as of 2.3, this is unpacked data for a packed array
                }
                else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
                         (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
                          expectedType == WireFormat.WireType.Fixed64))
                {
                    //Allowed as of 2.3, this is packed data for an unpacked array
                }
                else
                {
                    return(input.SkipField());
                }
            }
            if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type
            {
                return(input.SkipField());
            }

            switch (field.FieldType)
            {
            case FieldType.Group:
            case FieldType.Message:
            {
                if (!field.IsRepeated)
                {
                    IMessageLite message    = extensions[extension.Descriptor] as IMessageLite;
                    IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder();

                    if (field.FieldType == FieldType.Group)
                    {
                        input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessage(subBuilder, extensionRegistry);
                    }

                    extensions[field] = subBuilder.WeakBuild();
                }
                else
                {
                    List <IMessageLite> list = new List <IMessageLite>();
                    if (field.FieldType == FieldType.Group)
                    {
                        input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance,
                                             extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance,
                                               extensionRegistry);
                    }

                    foreach (IMessageLite m in list)
                    {
                        extensions.AddRepeatedField(field, m);
                    }
                    return(true);
                }
                break;
            }

            case FieldType.Enum:
            {
                if (!field.IsRepeated)
                {
                    object    unknown;
                    IEnumLite value = null;
                    if (input.ReadEnum(ref value, out unknown, field.EnumType))
                    {
                        extensions[field] = value;
                    }
                }
                else
                {
                    ICollection <object> unknown;
                    List <IEnumLite>     list = new List <IEnumLite>();
                    input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);

                    foreach (IEnumLite en in list)
                    {
                        extensions.AddRepeatedField(field, en);
                    }
                }
                break;
            }

            default:
            {
                if (!field.IsRepeated)
                {
                    object value = null;
                    if (input.ReadPrimitiveField(field.FieldType, ref value))
                    {
                        extensions[field] = value;
                    }
                }
                else
                {
                    List <object> list = new List <object>();
                    input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
                    foreach (object oval in list)
                    {
                        extensions.AddRepeatedField(field, oval);
                    }
                }
                break;
            }
            }

            return(true);
        }
Beispiel #10
0
 public bool IsValidValue(IEnumLite value)
 {
     return(items.ContainsKey(value.Number));
 }
Beispiel #11
0
        public bool IsValidValue(IEnumLite value)
        {
            TEnum val = default(TEnum);

            return(EnumParser <TEnum> .TryConvert(value.Number, ref val));
        }
Beispiel #12
0
 public bool IsValidValue(IEnumLite value)
 {
     return(Enum.IsDefined(typeof(TEnum), value.Number));
 }
Beispiel #13
0
        private static void VerifyType(IFieldDescriptorLite field, object value)
        {
            ThrowHelper.ThrowIfNull(value, "value");
            bool flag = false;

            switch (field.MappedType)
            {
            case MappedType.Int32:
                flag = (value is int);
                break;

            case MappedType.Int64:
                flag = (value is long);
                break;

            case MappedType.UInt32:
                flag = (value is uint);
                break;

            case MappedType.UInt64:
                flag = (value is ulong);
                break;

            case MappedType.Single:
                flag = (value is float);
                break;

            case MappedType.Double:
                flag = (value is double);
                break;

            case MappedType.Boolean:
                flag = (value is bool);
                break;

            case MappedType.String:
                flag = (value is string);
                break;

            case MappedType.ByteString:
                flag = (value is ByteString);
                break;

            case MappedType.Message:
            {
                IMessageLite messageLite = value as IMessageLite;
                flag = (messageLite != null);
                break;
            }

            case MappedType.Enum:
            {
                IEnumLite enumLite = value as IEnumLite;
                flag = (enumLite != null && field.EnumType.IsValidValue(enumLite));
                break;
            }
            }
            if (!flag)
            {
                string text = "Wrong object type used with protocol message reflection.";
                throw new ArgumentException(text);
            }
        }
        protected override bool ParseUnknownField(ICodedInputStream input, ExtensionRegistry extensionRegistry, uint tag, string fieldName)
        {
            TMessage messageBeingBuilt = this.MessageBeingBuilt;
            FieldSet extensions        = messageBeingBuilt.Extensions;

            WireFormat.WireType tagWireType = WireFormat.GetTagWireType(tag);
            int tagFieldNumber = WireFormat.GetTagFieldNumber(tag);
            IGeneratedExtensionLite generatedExtensionLite = extensionRegistry[this.DefaultInstanceForType, tagFieldNumber];

            if (generatedExtensionLite == null)
            {
                return(input.SkipField());
            }
            IFieldDescriptorLite descriptor = generatedExtensionLite.Descriptor;

            if (descriptor == null)
            {
                return(input.SkipField());
            }
            WireFormat.WireType wireType = descriptor.IsPacked ? WireFormat.WireType.LengthDelimited : WireFormat.GetWireType(descriptor.FieldType);
            if (tagWireType != wireType)
            {
                wireType = WireFormat.GetWireType(descriptor.FieldType);
                if (tagWireType != wireType && (!descriptor.IsRepeated || tagWireType != WireFormat.WireType.LengthDelimited || (wireType != WireFormat.WireType.Varint && wireType != WireFormat.WireType.Fixed32 && wireType != WireFormat.WireType.Fixed64)))
                {
                    return(input.SkipField());
                }
            }
            if (!descriptor.IsRepeated && tagWireType != WireFormat.GetWireType(descriptor.FieldType))
            {
                return(input.SkipField());
            }
            FieldType fieldType = descriptor.FieldType;

            switch (fieldType)
            {
            case FieldType.Group:
            case FieldType.Message:
            {
                if (descriptor.IsRepeated)
                {
                    List <IMessageLite> list = new List <IMessageLite>();
                    if (descriptor.FieldType == FieldType.Group)
                    {
                        input.ReadGroupArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry);
                    }
                    else
                    {
                        input.ReadMessageArray <IMessageLite>(tag, fieldName, list, generatedExtensionLite.MessageDefaultInstance, extensionRegistry);
                    }
                    using (List <IMessageLite> .Enumerator enumerator = list.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            IMessageLite current = enumerator.Current;
                            extensions.AddRepeatedField(descriptor, current);
                        }
                    }
                    return(true);
                }
                IMessageLite messageLite = extensions[generatedExtensionLite.Descriptor] as IMessageLite;
                IBuilderLite builderLite = (messageLite ?? generatedExtensionLite.MessageDefaultInstance).WeakToBuilder();
                if (descriptor.FieldType == FieldType.Group)
                {
                    input.ReadGroup(descriptor.FieldNumber, builderLite, extensionRegistry);
                }
                else
                {
                    input.ReadMessage(builderLite, extensionRegistry);
                }
                extensions[descriptor] = builderLite.WeakBuild();
                break;
            }

            default:
                if (fieldType == FieldType.Enum)
                {
                    if (!descriptor.IsRepeated)
                    {
                        IEnumLite value = null;
                        object    obj;
                        if (input.ReadEnum(ref value, out obj, descriptor.EnumType))
                        {
                            extensions[descriptor] = value;
                            break;
                        }
                        break;
                    }
                    else
                    {
                        List <IEnumLite>     list2 = new List <IEnumLite>();
                        ICollection <object> collection;
                        input.ReadEnumArray(tag, fieldName, list2, out collection, descriptor.EnumType);
                        using (List <IEnumLite> .Enumerator enumerator2 = list2.GetEnumerator())
                        {
                            while (enumerator2.MoveNext())
                            {
                                IEnumLite current2 = enumerator2.Current;
                                extensions.AddRepeatedField(descriptor, current2);
                            }
                            break;
                        }
                    }
                }
                if (!descriptor.IsRepeated)
                {
                    object value2 = null;
                    if (input.ReadPrimitiveField(descriptor.FieldType, ref value2))
                    {
                        extensions[descriptor] = value2;
                    }
                }
                else
                {
                    List <object> list3 = new List <object>();
                    input.ReadPrimitiveArray(descriptor.FieldType, tag, fieldName, list3);
                    using (List <object> .Enumerator enumerator3 = list3.GetEnumerator())
                    {
                        while (enumerator3.MoveNext())
                        {
                            object current3 = enumerator3.Current;
                            extensions.AddRepeatedField(descriptor, current3);
                        }
                    }
                }
                break;
            }
            return(true);
        }
Beispiel #15
0
 /// <summary>
 /// Logic moved from FieldSet to continue current behavior
 /// </summary>
 public bool IsValidValue(IEnumLite value)
 {
     return(value is EnumValueDescriptor && ((EnumValueDescriptor)value).EnumDescriptor == this);
 }
Beispiel #16
0
            /// <summary>
            /// Like <see cref="MergeFrom(ICodedInputStream, ExtensionRegistry, IBuilder)" />
            /// but parses a single field.
            /// </summary>
            /// <param name="input">The input to read the field from</param>
            /// <param name="extensionRegistry">Registry to use when an extension field is encountered</param>
            /// <param name="builder">Builder to merge field into, if it's a known field</param>
            /// <param name="tag">The tag, which should already have been read from the input</param>
            /// <returns>true unless the tag is an end-group tag</returns>
            internal bool MergeFieldFrom(ICodedInputStream input,
                                         ExtensionRegistry extensionRegistry, IBuilder builder, uint tag,
                                         string fieldName)
            {
                if (tag == 0 && fieldName != null)
                {
                    FieldDescriptor fieldByName = builder.DescriptorForType.FindFieldByName(fieldName);
                    if (fieldByName != null)
                    {
                        tag = WireFormat.MakeTag(fieldByName);
                    }
                    else
                    {
                        ExtensionInfo extension = extensionRegistry.FindByName(builder.DescriptorForType, fieldName);
                        if (extension != null)
                        {
                            tag = WireFormat.MakeTag(extension.Descriptor);
                        }
                    }
                }

                MessageDescriptor type = builder.DescriptorForType;

                if (type.Options.MessageSetWireFormat && tag == WireFormat.MessageSetTag.ItemStart)
                {
                    MergeMessageSetExtensionFromCodedStream(input, extensionRegistry, builder);
                    return(true);
                }

                WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
                int fieldNumber = WireFormat.GetTagFieldNumber(tag);

                FieldDescriptor field;
                IMessageLite    defaultFieldInstance = null;

                if (type.IsExtensionNumber(fieldNumber))
                {
                    ExtensionInfo extension = extensionRegistry[type, fieldNumber];
                    if (extension == null)
                    {
                        field = null;
                    }
                    else
                    {
                        field = extension.Descriptor;
                        defaultFieldInstance = extension.DefaultInstance;
                    }
                }
                else
                {
                    field = type.FindFieldByNumber(fieldNumber);
                }

                // Unknown field or wrong wire type. Skip.
                if (field == null)
                {
                    return(MergeFieldFrom(tag, input));
                }
                if (wireType != WireFormat.GetWireType(field))
                {
                    WireFormat.WireType expectedType = WireFormat.GetWireType(field.FieldType);
                    if (wireType == expectedType)
                    {
                        //Allowed as of 2.3, this is unpacked data for a packed array
                    }
                    else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
                             (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
                              expectedType == WireFormat.WireType.Fixed64))
                    {
                        //Allowed as of 2.3, this is packed data for an unpacked array
                    }
                    else
                    {
                        return(MergeFieldFrom(tag, input));
                    }
                }

                switch (field.FieldType)
                {
                case FieldType.Group:
                case FieldType.Message:
                {
                    IBuilderLite subBuilder = (defaultFieldInstance != null)
                                                          ? defaultFieldInstance.WeakCreateBuilderForType()
                                                          : builder.CreateBuilderForField(field);
                    if (!field.IsRepeated)
                    {
                        subBuilder.WeakMergeFrom((IMessageLite)builder[field]);
                        if (field.FieldType == FieldType.Group)
                        {
                            input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
                        }
                        else
                        {
                            input.ReadMessage(subBuilder, extensionRegistry);
                        }
                        builder[field] = subBuilder.WeakBuild();
                    }
                    else
                    {
                        List <IMessageLite> list = new List <IMessageLite>();
                        if (field.FieldType == FieldType.Group)
                        {
                            input.ReadGroupArray(tag, fieldName, list, subBuilder.WeakDefaultInstanceForType,
                                                 extensionRegistry);
                        }
                        else
                        {
                            input.ReadMessageArray(tag, fieldName, list, subBuilder.WeakDefaultInstanceForType,
                                                   extensionRegistry);
                        }

                        foreach (IMessageLite m in list)
                        {
                            builder.WeakAddRepeatedField(field, m);
                        }
                        return(true);
                    }
                    break;
                }

                case FieldType.Enum:
                {
                    if (!field.IsRepeated)
                    {
                        object    unknown;
                        IEnumLite value = null;
                        if (input.ReadEnum(ref value, out unknown, field.EnumType))
                        {
                            builder[field] = value;
                        }
                        else if (unknown is int)
                        {
                            MergeVarintField(fieldNumber, (ulong)(int)unknown);
                        }
                    }
                    else
                    {
                        ICollection <object> unknown;
                        List <IEnumLite>     list = new List <IEnumLite>();
                        input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);

                        foreach (IEnumLite en in list)
                        {
                            builder.WeakAddRepeatedField(field, en);
                        }

                        if (unknown != null)
                        {
                            foreach (object oval in unknown)
                            {
                                if (oval is int)
                                {
                                    MergeVarintField(fieldNumber, (ulong)(int)oval);
                                }
                            }
                        }
                    }
                    break;
                }

                default:
                {
                    if (!field.IsRepeated)
                    {
                        object value = null;
                        if (input.ReadPrimitiveField(field.FieldType, ref value))
                        {
                            builder[field] = value;
                        }
                    }
                    else
                    {
                        List <object> list = new List <object>();
                        input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
                        foreach (object oval in list)
                        {
                            builder.WeakAddRepeatedField(field, oval);
                        }
                    }
                    break;
                }
                }
                return(true);
            }
Beispiel #17
0
        /// <summary>
        /// Verifies that the given object is of the correct type to be a valid
        /// value for the given field.
        /// </summary>
        /// <remarks>
        /// For repeated fields, this checks if the object is of the right
        /// element type, not whether it's a list.
        /// </remarks>
        /// <exception cref="ArgumentException">The value is not of the right type.</exception>
        /// <exception cref="ArgumentNullException">The value is null.</exception>
        private static void VerifyType(IFieldDescriptorLite field, object value)
        {
            ThrowHelper.ThrowIfNull(value, "value");
            bool isValid = false;

            switch (field.MappedType)
            {
            case MappedType.Int32:
                isValid = value is int;
                break;

            case MappedType.Int64:
                isValid = value is long;
                break;

            case MappedType.UInt32:
                isValid = value is uint;
                break;

            case MappedType.UInt64:
                isValid = value is ulong;
                break;

            case MappedType.Single:
                isValid = value is float;
                break;

            case MappedType.Double:
                isValid = value is double;
                break;

            case MappedType.Boolean:
                isValid = value is bool;
                break;

            case MappedType.String:
                isValid = value is string;
                break;

            case MappedType.ByteString:
                isValid = value is ByteString;
                break;

            case MappedType.Enum:
                IEnumLite enumValue = value as IEnumLite;
                isValid = enumValue != null && field.EnumType.IsValidValue(enumValue);
                break;

            case MappedType.Message:
                IMessageLite messageValue = value as IMessageLite;
                isValid = messageValue != null;
                break;
            }

            if (!isValid)
            {
                // When chaining calls to SetField(), it can be hard to tell from
                // the stack trace which exact call failed, since the whole chain is
                // considered one line of code.  So, let's make sure to include the
                // field name and other useful info in the exception.
                string message = "Wrong object type used with protocol message reflection.";
                throw new ArgumentException(message);
            }
        }