/// <summary> /// Deserialize Object List /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> private void Deserialize <T>(List <T> obj) { var t = typeof(T); // Iterate through each value on Redis. foreach (string redisValue in RedisDatabase.SortedSetRangeByScore(t.GetTypeInfo().Name)) { var tObject = (T)Activator.CreateInstance(t); var hashEntries = RedisDatabase.HashGetAll(redisValue); foreach (var currentProperty in t.GetProperties()) { // Try to determine the property. try { if (currentProperty.PropertyType == typeof(int)) { currentProperty.SetValue( tObject, JsonConvert.DeserializeObject <int>( hashEntries.First(x => x.Name == currentProperty.Name).Value ) ); } else if (currentProperty.PropertyType == typeof(List <string>)) { currentProperty.SetValue( tObject, JsonConvert.DeserializeObject <List <string> >(hashEntries .First(x => x.Name == currentProperty.Name).Value ) ); } else { currentProperty.SetValue( tObject, JsonConvert.DeserializeObject( hashEntries.First(x => x.Name == currentProperty.Name).Value ) ); } } catch { // ignored } } // Add the current tObject obj.Add(tObject); } }
public static Dictionary <string, string> HashGetAll(string key) { if (!IsEnable) { throw new PlatformNotSupportedException("No Redis enable"); } var data = RedisDatabase.HashGetAll(key); Dictionary <string, string> temp = new Dictionary <string, string>(); foreach (var d in data) { temp.Add(d.Name, d.Value); } return(temp); }
/// <summary> /// Deserialize Object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> private void Deserialize <T>(T obj) { var t = typeof(T); var hashEntries = RedisDatabase.HashGetAll(t.Name); foreach (var property in t.GetProperties()) { try { if (property.PropertyType == typeof(int)) { property.SetValue( obj, JsonConvert.DeserializeObject <int>( hashEntries.First(x => x.Name == property.Name).Value ) ); } else if (property.PropertyType == typeof(List <string>)) { property.SetValue( obj, JsonConvert.DeserializeObject <List <string> >( hashEntries.First(x => x.Name == property.Name).Value ) ); } else { property.SetValue( obj, JsonConvert.DeserializeObject( hashEntries.First(x => x.Name == property.Name).Value ) ); } } catch { // ignored } } }