检查,看是否有属性C#为Expando类中存在 [英] check to see if a property exists within a C# Expando class
本文介绍了检查,看是否有属性C#为Expando类中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想看看,如果一个属性在C#为Expando类存在。
很像 hasattr Python函数中。我想对于hasattr C#的equalant。
这样的事情...
如果(HasAttr(型号,ID))
{
#做点什么model.Id
}
解决方案
尝试:
动态yourExpando =新ExpandoObject();
如果(((IDictionary的<字符串对象>)yourExpando).ContainsKey(ID))
{
//拥有财产......
}
这是ExpandoObject明确实施的IDictionary<字符串对象>
,其中最关键的是属性名。然后,您可以检查,看看是否字典中包含的关键。你也可以写一个小的辅助方法,如果你需要经常做这类检查的:
私有静态布尔HasAttr(ExpandoObject的expando ,串键)
{
回报率((IDictionary的<字符串对象>)的expando).ContainsKey(密钥);
}
和使用它像这样:
如果(HasAttr(yourExpando,ID))
{
//拥有财产......
}
I would like to see if a property exist in a C# Expando Class.
much like the hasattr function in python. I would like the c# equalant for hasattr.
something like this...
if (HasAttr(model, "Id"))
{
# Do something with model.Id
}
解决方案
Try:
dynamic yourExpando = new ExpandoObject();
if (((IDictionary<string, Object>)yourExpando).ContainsKey("Id"))
{
//Has property...
}
An ExpandoObject explicitly implements IDictionary<string, Object>
, where the Key is a property name. You can then check to see if the dictionary contains the key. You can also write a little helper method if you need to do this kind of check often:
private static bool HasAttr(ExpandoObject expando, string key)
{
return ((IDictionary<string, Object>) expando).ContainsKey(key);
}
And use it like so:
if (HasAttr(yourExpando, "Id"))
{
//Has property...
}
这篇关于检查,看是否有属性C#为Expando类中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文