コード例 #1
0
        public override List <StockPriceSlim> DeserializeSlim(byte[] source)
        {
            var result = new List <StockPriceSlim>();
            var index  = 0;

            using (var stream = new MemoryStream(source))
            {
                while (index < source.Length)
                {
                    var price = new StockPriceSlim();
                    var bytes = new byte[sizeof(short)];
                    stream.Read(bytes, 0, sizeof(short));
                    var days = BitConverter.ToInt16(bytes, 0);
                    price.DaysFrom1970 = days;
                    index += bytes.Length;

                    bytes = new byte[sizeof(float)];
                    stream.Read(bytes, 0, sizeof(float));
                    var value = BitConverter.ToSingle(bytes, 0);
                    price.OpenPrice = value;
                    index          += bytes.Length;

                    stream.Read(bytes, 0, sizeof(float));
                    value           = BitConverter.ToSingle(bytes, 0);
                    price.HighPrice = value;
                    index          += bytes.Length;

                    stream.Read(bytes, 0, sizeof(float));
                    value          = BitConverter.ToSingle(bytes, 0);
                    price.LowPrice = value;
                    index         += bytes.Length;

                    stream.Read(bytes, 0, sizeof(float));
                    value            = BitConverter.ToSingle(bytes, 0);
                    price.ClosePrice = value;
                    index           += bytes.Length;

                    stream.Read(bytes, 0, sizeof(float));
                    value = BitConverter.ToSingle(bytes, 0);
                    price.PrvClosePrice = value;
                    index += bytes.Length;

                    bytes = new byte[sizeof(double)];
                    stream.Read(bytes, 0, sizeof(double));
                    var volume = BitConverter.ToDouble(bytes, 0);
                    price.Volume = volume;
                    index       += bytes.Length;

                    bytes = new byte[sizeof(double)];
                    stream.Read(bytes, 0, sizeof(double));
                    var turnover = BitConverter.ToDouble(bytes, 0);
                    price.Turnover = turnover;
                    index         += bytes.Length;

                    result.Add(price);
                }
                return(result);
            }
        }
コード例 #2
0
		public override List<StockPriceSlim> DeserializeSlim(byte[] source)
		{
			using (var stream = new MemoryStream(source))
			{
				var result = new List<StockPriceSlim>();
				var index = 0;

				while (index < source.Length)
				{
					var price = new StockPriceSlim();
					foreach (var property in typeof(StockPriceSlim).GetProperties())
					{
						if (property.GetCustomAttribute(typeof(DataMemberAttribute)) == null)
							continue;

						byte[] bytes = null;
						object value = null;

						if (property.PropertyType == typeof(int))
						{
							bytes = new byte[sizeof(int)];
							stream.Read(bytes, 0, bytes.Length);
							value = BitConverter.ToInt32(bytes, 0);
						}
						else if (property.PropertyType == typeof(short))
						{
							bytes = new byte[sizeof(short)];
							stream.Read(bytes, 0, bytes.Length);
							value = BitConverter.ToInt16(bytes, 0);
						}
						else if (property.PropertyType == typeof(float))
						{
							bytes = new byte[sizeof(float)];
							stream.Read(bytes, 0, bytes.Length);
							value = BitConverter.ToSingle(bytes, 0);
						}
						else if (property.PropertyType == typeof(double))
						{
							bytes = new byte[sizeof(double)];
							stream.Read(bytes, 0, bytes.Length);
							value = BitConverter.ToDouble(bytes, 0);
						}

						property.SetValue(price, value);
						index += bytes.Length;
					}


					result.Add(price);
				}
				return result;
			}
		}