public T Read <T>() { var type = typeof(T).IsEnum ? typeof(T).GetEnumUnderlyingType() : typeof(T); object value; switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: value = Stream.ReadBoolean(); break; case TypeCode.SByte: value = Stream.ReadSByte(); break; case TypeCode.Byte: value = Stream.ReadByte(); break; case TypeCode.Char: value = Stream.ReadChar(); break; case TypeCode.Int16: value = Stream.ReadInt16(); break; case TypeCode.UInt16: value = Stream.ReadUInt16(); break; case TypeCode.Int32: value = Stream.ReadInt32(); break; case TypeCode.UInt32: value = Stream.ReadUInt32(); break; case TypeCode.Int64: value = Stream.ReadInt64(); break; case TypeCode.UInt64: value = Stream.ReadUInt64(); break; case TypeCode.Single: value = Stream.ReadSingle(); break; case TypeCode.Double: value = Stream.ReadDouble(); break; default: throw new InvalidCastException(""); } return((T)value); }