private T GetEntityFromBytes()

in code/Server/CTStore/RedisCache.cs [1280:1391]


        private T GetEntityFromBytes<T>(byte[] value)
            where T : Entity, new()
        {
            if (value == null)
            {
                return null;
            }

            T entity = new T();
            MemoryStream stream = new MemoryStream(value);
            using (BinaryReader reader = new BinaryReader(stream))
            {
                if (typeof(FeedEntity).IsAssignableFrom(typeof(T)))
                {
                    List<byte> itemKeyBytes = new List<byte>();
                    byte itemKeyByte = reader.ReadByte();
                    while (itemKeyByte != 0)
                    {
                        itemKeyBytes.Add(itemKeyByte);
                        itemKeyByte = reader.ReadByte();
                    }

                    if (itemKeyBytes.Count > 0)
                    {
                        ((FeedEntity)(object)entity).ItemKey = UTF8Encoding.UTF8.GetString(itemKeyBytes.ToArray());
                    }
                }

                entity.CacheFlags = (CacheFlags)reader.ReadByte();
                if (entity.CacheInvalid)
                {
                    entity.CacheExpiry = DateTime.FromBinary(reader.ReadInt64());
                }

                if (!entity.NoETag)
                {
                    entity.ETag = reader.ReadString();
                }
                else
                {
                    entity.ETag = this.ConvertValueToETag(value);
                }

                if (entity.CacheInvalid)
                {
                    return entity;
                }

                var properties = entity.GetType().GetProperties();
                var sortedProperties = properties.OrderBy(p => p.Name);

                foreach (var property in sortedProperties)
                {
                    if (SpecialFieldNames.IsSpecialFieldName(property.Name, typeof(T)))
                    {
                        continue;
                    }

                    if (property.PropertyType == typeof(string))
                    {
                        bool notNull = reader.ReadBoolean();
                        if (notNull)
                        {
                            property.SetValue(entity, reader.ReadString());
                        }
                    }
                    else if (property.PropertyType == typeof(int))
                    {
                        property.SetValue(entity, reader.ReadInt32());
                    }
                    else if (property.PropertyType == typeof(long))
                    {
                        property.SetValue(entity, reader.ReadInt64());
                    }
                    else if (property.PropertyType == typeof(double))
                    {
                        property.SetValue(entity, reader.ReadDouble());
                    }
                    else if (property.PropertyType == typeof(bool))
                    {
                        property.SetValue(entity, reader.ReadBoolean());
                    }
                    else if (property.PropertyType == typeof(DateTime))
                    {
                        property.SetValue(entity, DateTime.FromBinary(reader.ReadInt64()));
                    }
                    else if (property.PropertyType.BaseType == typeof(Enum))
                    {
                        property.SetValue(entity, Enum.Parse(property.PropertyType, reader.ReadString()));
                    }
                    else if (property.PropertyType == typeof(byte[]))
                    {
                        int size = reader.ReadInt32();
                        if (size == 0)
                        {
                            property.SetValue(entity, new byte[0]);
                        }

                        if (size > 0)
                        {
                            property.SetValue(entity, reader.ReadBytes(size));
                        }
                    }
                    else if (property.PropertyType == typeof(byte))
                    {
                        property.SetValue(entity, reader.ReadByte());
                    }
                }
            }

            return entity;
        }