private bool WriteValue(Type dataType, SuperStream stream, object value) { if (dataType == typeof(Byte)) { stream.WriteByte((byte)value); return(true); } if (dataType == typeof(Int64)) { stream.WriteInt64((Int64)value); return(true); } if (dataType == typeof(UInt64)) { stream.WriteUInt64((UInt64)value); return(true); } if (dataType == typeof(Int32)) { stream.WriteInt32((Int32)value); return(true); } if (dataType == typeof(UInt32)) { stream.WriteUInt32((UInt32)value); return(true); } if (dataType == typeof(Int16)) { stream.WriteInt16((Int16)value); return(true); } if (dataType == typeof(UInt16)) { stream.WriteUInt16((UInt16)value); return(true); } if (dataType == typeof(Single)) { stream.WriteSingle((Single)value); return(true); } if (dataType == typeof(Double)) { stream.WriteDouble((Double)value); return(true); } if (dataType == typeof(bool)) { // If bool is true, write 1. Otherwise, write 0. int val = ((bool)value) ? 1 : 0; stream.WriteInt32(val); } if (dataType.IsEnum) { dynamic storeValue = Convert.ChangeType(value, Enum.GetUnderlyingType(dataType)); return(this.WriteValue(storeValue.GetType(), stream, storeValue)); } // Test if type has StreamDataAttribute on properties. // This allows nesting of StreamData-aware task.DataTypes var props = StreamDataInfo.GetProperties(dataType); if (props.Length == 0) { return(false); } StreamData.Serialize(value, stream); // Need to add error condition here. return(true); }
private bool GetValue(Type dataType, SuperStream stream, out object value) { if (dataType == typeof(Byte)) { value = (byte)stream.ReadByte(); return(true); } if (dataType == typeof(Int64)) { value = stream.ReadInt64(); return(true); } if (dataType == typeof(UInt64)) { value = stream.ReadUInt64(); return(true); } if (dataType == typeof(Int32)) { value = stream.ReadInt32(); return(true); } if (dataType == typeof(UInt32)) { value = stream.ReadUInt32(); return(true); } if (dataType == typeof(Int16)) { value = stream.ReadInt16(); return(true); } if (dataType == typeof(UInt16)) { value = stream.ReadUInt16(); return(true); } if (dataType == typeof(Single)) { value = stream.ReadSingle(); return(true); } if (dataType == typeof(Double)) { value = stream.ReadDouble(); return(true); } if (dataType == typeof(bool)) { var val = stream.ReadUInt32(); if (val == 0) { value = false; return(true); } else if (val == 1) { value = true; return(true); } else { throw new Exception("Parsing type bool: Expected value to be either 0 or 1, but it was " + val.ToString()); } } if (dataType.IsEnum) { object readValue; // Read the enums underlying type if (!this.GetValue(Enum.GetUnderlyingType(dataType), stream, out readValue)) { value = null; return(false); } // Parse enum using the read value. value = Enum.ToObject(dataType, readValue); return(true); } // Test if type has StreamDataAttribute on properties. // This allows nesting of StreamData-aware task.DataTypes var props = StreamDataInfo.GetProperties(dataType); if (props.Length == 0) { value = null; return(false); } value = StreamData.Create(dataType, stream); if (value == null) { return(false); } return(true); }