Example #1
0
        private static MessageField Create(TagAttribute attr, IFieldIO io)
        {
            int tag = attr.Number;

            if (attr.Fixed)
            {
                return(CreateFixed(tag, io));
            }

            if (attr.ZigZag)
            {
                return(CreateZigZag(tag, io));
            }

            Type type = io.FieldType;

            if (type.EqualsAny(typeof(int), typeof(long),
                               typeof(bool), typeof(uint), typeof(ulong)))
            {
                return(new MessageFieldVarint(tag, io));
            }

            if (type == typeof(string))
            {
                return(new MessageFieldString(tag, io));
            }

            if (type == typeof(byte[]))
            {
                return(new MessageFieldBytes(tag, io));
            }

            if (type == typeof(float))
            {
                return(new MessageFieldFixed(tag, io, WireType.Fixed32));
            }

            if (type == typeof(double))
            {
                return(new MessageFieldFixed(tag, io, WireType.Fixed64));
            }

            if (type.IsEnum)
            {
                return(new MessageFieldEnum(tag, io));
            }

            if (type == typeof(DateTime) || type == typeof(Decimal))
            {
                return(new MessageField(tag, io));
            }

            if (type == typeof(short) || type == typeof(ushort))
            {
                return(new MessageFieldInt16(tag, io));
            }

            return(new MessageFieldObject(tag, io));
        }
Example #2
0
        public static MessageField Create(TagAttribute attr, PropertyInfo property)
        {
            IFieldIO fieldIO;

            if (attr.Packed && PackedFieldIO.TryCreate(property, out fieldIO))
            {
                return(new MessageFieldPacked(Create(attr, fieldIO)));
            }

            return(Create(attr, CreateFieldIO(property)));
        }
Example #3
0
        public static void ForEachField(Type messageType, Action <MessageField> action)
        {
            PropertyInfo[]      properties = messageType.GetProperties();
            List <MessageField> fields     = new List <MessageField>(properties.Length);

            properties.ForEach((field, index) => {
                TagAttribute attribute = field
                                         .GetCustomAttributes <TagAttribute>(false)
                                         .FirstOrDefault();

                if (attribute != null)
                {
                    fields.Add(MessageField.Create(attribute, field));
                }
            });

            fields.Sort((x, y) => x.Number - y.Number);
            fields.ForEach(action);
        }