【发布时间】:2016-12-26 16:15:43
【问题描述】:
我已经通过以下方法将我的类值插入到 redis 中
public static bool InsertHashItem(string key, object obj)
{
bool result = false;
try
{
if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
{
IDatabase getDatabase = Muxer.GetDatabase();
getDatabase.HashSet(key, ToHashEntries(obj));
result = true;
}
}
catch (Exception ex)
{
Logger.Fatal(ex.Message, ex);
}
return result;
}
我的班级看起来像
public class TestType
{
public int telcoid { get; set; }
public int parent_id { get; set; }
public string checksum { get; set; }
public string tag_registration_id { get; set; }
public string market_id { get; set; }
public double amount { get; set; }
public string utid { get; set; }
public int status { get; set; }
public DateTime? dt { get; set; }
}
现在为了获得那个类,我编写了一个在可空类型上失败的方法
public static T GetHashItem<T>(string key) where T : new()
{
T obj = default(T);
try
{
if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
{
IDatabase getDatabase = Muxer.GetDatabase();
var hashkey = getDatabase.HashKeys(key);
if (hashkey != null && hashkey.Count() > 0)
{
var dic = hashkey.ToDictionary(k => k, k => getDatabase.HashGet(key, k));
obj = new T();
foreach (var prop in typeof(T).GetProperties())
{
prop.SetValue(obj, Convert.ChangeType(dic[prop.Name], prop.PropertyType));
}
}
}
}
catch (Exception ex)
{
Logger.Fatal(ex.Message, ex);
}
return obj;
}
请告知我需要什么更正
【问题讨论】:
标签: c#-4.0 redis .net-4.5 system.reflection stackexchange.redis