/// <summary> /// /// </summary> /// <param name="o">int long float double bool string Imessage </param> public void AddParam(object o) { byte[] bytes = null; if (o is int) { int length = CodedOutputStream.ComputeInt32Size((int)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteInt32((int)o); } else if (o is long) { int length = CodedOutputStream.ComputeInt64Size((long)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteInt64((long)o); } else if (o is float) { int length = CodedOutputStream.ComputeFloatSize((float)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteFloat((float)o); } else if (o is double) { int length = CodedOutputStream.ComputeDoubleSize((double)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteDouble((double)o); } else if (o is bool) { int length = CodedOutputStream.ComputeBoolSize((bool)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteBool((bool)o); } else if (o is string) { int length = CodedOutputStream.ComputeStringSize((string)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteString((string)o); } else if (o is IMessage) { int length = CodedOutputStream.ComputeMessageSize((IMessage)o); bytes = new byte[length]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteMessage((IMessage)o); } if (bytes != null) { AddParam(o.GetType().FullName, bytes); } }
public void Boolean_Test() { var codeGenerator = new BooleanCodeGenerator(); var value = true; Assert.Equal(CodedOutputStream.ComputeBoolSize(true), codeGenerator.CalculateSize(value)); var fieldCodec = codeGenerator.CreateFieldCodec(1); var stream = new MemoryStream(); var output = new CodedOutputStream(stream, true); fieldCodec.WriteTagAndValue(output, value); output.Flush(); stream.Position = 0; var input = new CodedInputStream(stream); Assert.Equal(WireFormat.MakeTag(1, codeGenerator.WireType), input.ReadTag()); Assert.Equal(value, fieldCodec.Read(input)); }
int GetLengthOf <T>(T instance, MessageDescription messageDescription) { var size = 0; messageDescription.Properties.ForEach(property => { var type = property.Property.PropertyType; var number = property.Number; var value = property.Property.GetValue(instance); if (_valueConverters.CanConvert(type)) { var converter = _valueConverters.GetConverterFor(type); type = converter.SerializedAs(type); value = converter.ConvertTo(value); } size += CodedOutputStream.ComputeTagSize(number); if (type == typeof(Guid)) { var guidAsBytes = ((Guid)value).ToByteArray(); var length = CodedOutputStream.ComputeBytesSize(ByteString.CopyFrom(guidAsBytes)); size += CodedOutputStream.ComputeLengthSize(length); size += length; } else if (type == typeof(string)) { var valueAsString = value as string; var length = CodedOutputStream.ComputeStringSize(valueAsString); size += CodedOutputStream.ComputeLengthSize(length); size += length; } else if (type == typeof(int)) { size += CodedOutputStream.ComputeInt32Size((int)value); } else if (type == typeof(Int64)) { size += CodedOutputStream.ComputeInt64Size((Int64)value); } else if (type == typeof(uint)) { size += CodedOutputStream.ComputeUInt32Size((uint)value); } else if (type == typeof(UInt64)) { size += CodedOutputStream.ComputeUInt64Size((UInt64)value); } else if (type == typeof(float)) { size += CodedOutputStream.ComputeFloatSize((float)value); } else if (type == typeof(double)) { size += CodedOutputStream.ComputeDoubleSize((double)value); } else if (type == typeof(bool)) { size += CodedOutputStream.ComputeBoolSize((bool)value); } else if (type == typeof(DateTimeOffset)) { size += CodedOutputStream.ComputeInt64Size(((DateTimeOffset)value).ToUnixTimeMilliseconds()); } else if (type == typeof(DateTime)) { size += CodedOutputStream.ComputeInt64Size(new DateTimeOffset(((DateTime)value).ToUniversalTime()).ToUnixTimeMilliseconds()); } }); return(size); }
/// <inheritdoc/> public override int CalculateSize(bool value) { return(CodedOutputStream.ComputeBoolSize(value)); }