C# 从通用字典确定可为空的项
我有一本字典C# 从通用字典确定可为空的项,c#,generics,C#,Generics,我有一本字典 Dictionary<string, object> dict = new Dictionary<string, object>() { { "foo", 2.5 } }; 梅斯多德: // chagned Type to Nullable<Type> public static Nullable<Type> GetRes<Type>(Dictionary<string, object> dict, stri
Dictionary<string, object> dict = new Dictionary<string, object>() { { "foo", 2.5 } };
梅斯多德:
// chagned Type to Nullable<Type>
public static Nullable<Type> GetRes<Type>(Dictionary<string, object> dict, string key)
{
if (dict.ContainsKey(key) && dict[key].GetType() == typeof(Type))
{
return (Nullable<Type>)dict[key];
}
return default(Nullable<Type>);
}
//将类型更改为可空
公共静态可空GetRes(字典dict dict、字符串键)
{
if(dict.ContainsKey(key)&&dict[key].GetType()==typeof(Type))
{
返回(可为空)dict[key];
}
返回默认值(可为空);
}
这无法编译。为什么不检查dict[key]是您的类型:
public static Type GetRes<Type>(Dictionary<string, object> dict, string key)
{
if (dict.ContainsKey(key) && dict[key] is Type)
{
return (Type)dict[key];
}
return default(Type);
}
公共静态类型GetRes(Dictionary dict,string key)
{
if(dict.ContainsKey(键)&&dict[key]为类型)
{
返回(类型)dict[键];
}
返回默认值(类型);
}
下面的扩展方法应该可以做到这一点:
public static T GetValue<T>(this Dictionary<string, object> dictionary, string key)
{
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
if (!dictionary.ContainsKey(key))
{
return default(T); //Or null, depending on you needs
}
var value = dictionary[key];
if (value == null)
{
return default(T);
}
if (value is IConvertible)
{
return (T)Convert.ChangeType(value, typeof(T));
}
return (T)value;
}
publicstatict GetValue(这个字典,字符串键)
{
if(dictionary==null)
{
抛出新ArgumentNullException(nameof(dictionary));
}
如果(!dictionary.ContainsKey(键))
{
返回默认值(T);//或null,具体取决于您的需要
}
var值=字典[键];
如果(值==null)
{
返回默认值(T);
}
如果(值是可转换的)
{
return(T)Convert.ChangeType(value,typeof(T));
}
返回(T)值;
}
你可以这样称呼它:
var dict = new Dictionary<string, object>() {
{ "foo1", 2.5 },
{ "foo2", "my string" },
{ "foo3", new MyClass() },
{ "foo4", 1 },
{ "foo5", null }
};
var res1 = dict.GetValue<float>("foo1");
var res2 = dict.GetValue<string>("foo2");
var res3 = dict.GetValue<MyClass>("foo3");
var res4 = dict.GetValue<int>("foo4");
var res5 = dict.GetValue<int?>("foo5");
var dict=newdictionary(){
{“foo1”,2.5},
{“foo2”,“我的字符串”},
{“foo3”,新的MyClass()},
{“foo4”,1},
{“foo5”,null}
};
var res1=dict.GetValue(“foo1”);
var res2=dict.GetValue(“foo2”);
var res3=dict.GetValue(“foo3”);
var res4=dict.GetValue(“foo4”);
var res5=dict.GetValue(“foo5”);
对于仅结构类型,请尝试此方法签名:
public static T? GetValue<T>(this Dictionary<string, object> dictionary, string key) where T : struct
{
//implementation should be the same
}
publicstatict?GetValue(此字典,字符串键),其中T:struct
{
//实施应该是一样的
}
解决方案是添加其中Type:struct
,如下所示:
public static Type? GetRes2<Type>(Dictionary<string, object> dict, string key) where Type : struct
{
if (dict.ContainsKey(key) && dict[key].GetType() == typeof(Type))
{
return (Type?)dict[key];
}
return default(Type?);
}
公共静态类型?GetRes2(字典dict,字符串键),其中类型:struct
{
if(dict.ContainsKey(key)&&dict[key].GetType()==typeof(Type))
{
返回(类型?)dict[键];
}
返回默认值(类型?);
}
类型“type”必须是不可为空的值类型
,才能将其用作参数“T”。然后,您可以通过使用where Type:struct
强制“Type”为值类型
,您应该使用“is”检查对象是否可能被强制转换
public static Type GetRes<Type>(Dictionary<string, object> dict, string key)
{
if (dict.ContainsKey(key) && dict[key] is Type)
{
return (dict[key] as Type);
}
return default(Type);
}
公共静态类型GetRes(Dictionary dict,string key)
{
if(dict.ContainsKey(键)&&dict[key]为类型)
{
返回(dict[key]作为类型);
}
返回默认值(类型);
}
((对象)(int?)5).GetType()==typeof(int)
,而((对象)(int?)null)==null
。可空值类型的装箱就是这样工作的。你什么都不能做。如果值类型为null
,则装箱引用为null
,如果不是null
,则装箱可空类型的值。不可能有完整的可为空的装箱值。请参阅示例,您可以尝试使用dict[key]is Type为什么需要额外的dict[key].GetType()==typeof(Type)
check?如果您的字典确实包含请求的键,请继续并尝试将其强制转换为所需的类型,否则它将返回default(T)
@haim770default(T)
如果double
是0
,那么我不知道结果是0
还是找不到值只要删除dict[key].GetType()=typeof(键入)
检查并保持所有其他内容不变。不需要它。如果给定的键有一个值,但它的类型错误-您的程序有一个错误,您需要修复该错误,而不是将其隐藏在地毯下。默认值(双精度)
返回0
,我不知道键是否不存在,或者结果是0
在这种情况下,包括一个条件:if(!dict.ContainsKey(key)){return null;}默认值(double)
返回0
,我不知道键是否不存在,或者结果是0
如果键不存在,则行“var value=dictionary[key];”将引发异常。如果值不存在,则要求返回null
。请参阅我的更新答案。我已添加了一个检查以查看键是否存在。您还可以使用dictionary.TryGetValue。如果typet
为double
则不能返回null默认值(double)
返回0
,我不知道键是否不存在或者结果是否为0
返回默认值(类型?);
将始终为返回null;
是。可空值的默认值始终为null!
public static T? GetValue<T>(this Dictionary<string, object> dictionary, string key) where T : struct
{
//implementation should be the same
}
public static Type? GetRes2<Type>(Dictionary<string, object> dict, string key) where Type : struct
{
if (dict.ContainsKey(key) && dict[key].GetType() == typeof(Type))
{
return (Type?)dict[key];
}
return default(Type?);
}
public static Type GetRes<Type>(Dictionary<string, object> dict, string key)
{
if (dict.ContainsKey(key) && dict[key] is Type)
{
return (dict[key] as Type);
}
return default(Type);
}