コード例 #1
0
ファイル: CompactBinaryWriter.cs プロジェクト: ylyking/Medusa
 public override void OnValue <T>(T obj)
 {
     if (obj is bool)
     {
         Stream.WriteUInt8(Convert.ToByte(obj));
     }
     else if (obj is char)
     {
         Stream.WriteUInt8(Convert.ToByte(obj));
     }
     else if (obj is short)
     {
         Stream.WriteVarUInt16(IntegerHelper.EncodeZigzag(Convert.ToInt16(obj)));
     }
     else if (obj is int)
     {
         Stream.WriteVarUInt32(IntegerHelper.EncodeZigzag(Convert.ToInt32(obj)));
     }
     else if (obj is Int64)
     {
         Stream.WriteVarUInt64(IntegerHelper.EncodeZigzag(Convert.ToInt64(obj)));
     }
     else if (obj is byte)
     {
         Stream.WriteUInt8(Convert.ToByte(obj));
     }
     else if (obj is ushort)
     {
         Stream.WriteVarUInt16(Convert.ToUInt16(obj));
     }
     else if (obj is uint)
     {
         Stream.WriteVarUInt32(Convert.ToUInt32(obj));
     }
     else if (obj is UInt64)
     {
         Stream.WriteVarUInt64(Convert.ToUInt64(obj));
     }
     else if (obj is float)
     {
         Stream.WriteFloat((float)Convert.ToDouble(obj));
     }
     else if (obj is double)
     {
         Stream.WriteDouble(Convert.ToDouble(obj));
     }
     else
     {
         var type = obj.GetType();
         if (type.IsEnum)
         {
             Stream.WriteVarUInt32(Convert.ToUInt32(obj));
         }
         else
         {
             Console.WriteLine("Invalid value:{0} in type:{1}", obj, obj.GetType());
         }
     }
 }