Ejemplo n.º 1
0
        public override void WriteToStream(Stream stream)
        {
            var bytes  = Hex.HexToBytes(value);
            var length = new UnsignedInt((uint)bytes.Length);

            length.WriteToStream(stream);
            stream.Write(bytes, 0, bytes.Length);
        }
Ejemplo n.º 2
0
        public override void WriteToStream(Stream stream)
        {
            var bytes  = Encoding.UTF8.GetBytes(value);
            var length = new UnsignedInt((uint)bytes.Length);

            length.WriteToStream(stream);
            stream.Write(bytes, 0, bytes.Length);
        }
Ejemplo n.º 3
0
        public virtual void WritePropertyToStream(Stream stream, Type type, object value)
        {
            switch (value)
            {
            case bool item:
            {
                stream.WriteByte((byte)(item ? 1 : 0));
                return;
            }

            case byte item:
            {
                stream.WriteByte(item);
                return;
            }

            case short item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case ushort item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case int item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case uint item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case long item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case ulong item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case float item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case double item:
            {
                var bytes = BitConverter.GetBytes(item);
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case byte[] bytes:
            {
                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case string item:
            {
                if (string.IsNullOrEmpty(item))
                {
                    stream.WriteByte(0);
                    return;
                }
                var bytes = Encoding.UTF8.GetBytes(item);

                var length = new UnsignedInt((uint)bytes.Length);
                length.WriteToStream(stream);

                stream.Write(bytes, 0, bytes.Length);
                return;
            }

            case BaseCustomType item:
            {
                item.WriteToStream(stream);
                return;
            }

            default:
            {
                if (type.IsArray)
                {
                    var item = (ICollection)value;
                    if (item == null)
                    {
                        return;
                    }

                    var bytes = new UnsignedInt((uint)item.Count);
                    bytes.WriteToStream(stream);

                    foreach (var property in item)
                    {
                        WritePropertyToStream(stream, property.GetType(), property);
                    }
                    return;
                }
                if (type.IsClass)
                {
                    Type classType  = value.GetType();
                    var  properties = classType.GetRuntimeProperties();
                    foreach (var property in properties)
                    {
                        WriteToStream(stream, property, value);
                    }
                    return;
                }
                throw new NotImplementedException();
            }
            }
        }