Redis 将 HashSet 获取到所需的类型而不适用于可空类型
Redis Get HashSet into desired type not working with nullable type
我已经通过以下方法将 class 值插入到 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;
}
我的 class 看起来像
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; }
}
现在得到那个class我写了一个方法,它在可空类型上失败
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;
}
请指教我需要什么修正
当 属性 类型为 Nullable 时,尝试获取基础类型,如下所示:
Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
prop.SetValue(obj, Convert.ChangeType(dic[prop.Name], type));
我已经通过以下方法将 class 值插入到 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;
}
我的 class 看起来像
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; }
}
现在得到那个class我写了一个方法,它在可空类型上失败
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;
}
请指教我需要什么修正
当 属性 类型为 Nullable 时,尝试获取基础类型,如下所示:
Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
prop.SetValue(obj, Convert.ChangeType(dic[prop.Name], type));