Пример #1
0
        public static object ToObject(this ProtoFieldCodec field, ulong value)
        {
            object unboxedValue = null;

            switch (field.FieldDescriptor.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Bytes:
                unboxedValue = (byte)value;
                break;

            case Google.Protobuf.Reflection.FieldType.Int32:
            case Google.Protobuf.Reflection.FieldType.SInt32:
            case Google.Protobuf.Reflection.FieldType.UInt32:
                unboxedValue = (int)value;
                break;

            case Google.Protobuf.Reflection.FieldType.UInt64:
            case Google.Protobuf.Reflection.FieldType.SInt64:
            case Google.Protobuf.Reflection.FieldType.Int64:
                unboxedValue = (long)value;
                break;

            default:
                throw new NotSupportedException($"Unsupported type {field.FieldDescriptor.FieldType}");
            }

            return(unboxedValue);
        }
Пример #2
0
        public static long ToUnboxedNumber(this ProtoFieldCodec field, object value)
        {
            long unboxedInt = 0;

            switch (field.FieldDescriptor.FieldType)
            {
            case Google.Protobuf.Reflection.FieldType.Bool:
                unboxedInt = ((bool)value) ? 1 : 0;
                break;

            case Google.Protobuf.Reflection.FieldType.Int32:
            case Google.Protobuf.Reflection.FieldType.SInt32:
            case Google.Protobuf.Reflection.FieldType.UInt32:
                unboxedInt = (int)value;
                break;

            case Google.Protobuf.Reflection.FieldType.UInt64:
            case Google.Protobuf.Reflection.FieldType.SInt64:
            case Google.Protobuf.Reflection.FieldType.Int64:
                unboxedInt = (long)value;
                break;

            default:
                throw new NotSupportedException($"Unsupported type {field.FieldDescriptor.FieldType}");
            }

            return(unboxedInt);
        }