循环遍历属性和计数值 [英] looping through properties and count value
本文介绍了循环遍历属性和计数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
empsupp.isGoodCitizenCalculated;
empsupp.iscomplyWithAllRegReqCalculated;
empsupp.isIndustrySpendCalculated;
empsupp.isJobCreationCalculated;
empsupp.isMaterialTransCalculated;
empsupp.isSkillTransCalculated;
empsupp.isServiceIndLabCostCalculated;
empsupp class 以及上面列出的属性是 bool 的类型。
现在我必须返回基于此计算的整体状态: -------------
如果任何 3 最后一个 5 (注意:不是所有属性,只需要最后一个 5 )属性值找到 true ,然后在所有状态下 true ,否则 false 。
如果 else ,我本来可以做到的,但我认为 不正确,因为如果我必须检查 100 属性,那么?
解决方案
请检查
< pre lang =C#> public int CountBooleanValue( object 数据)
{
int count = 0 ;
foreach (PropertyInfo propertyInfo in data.GetType()。GetProperties()。Skip(< span class =code-digit> 5 ))
{
if (propertyInfo.PropertyType == typeof ( bool ))
{
bool value = Convert.ToBoolean(propertyInfo.GetValue(data, null ));
if ( value == true )
{
count ++;
}
}
}
return count;
}
empsupp.isGoodCitizenCalculated ;
empsupp.iscomplyWithAllRegReqCalculated;
empsupp.isIndustrySpendCalculated;
empsupp.isJobCreationCalculated ;
empsupp.isMaterialTransCalculated ;
empsupp.isSkillTransCalculated;
empsupp.isServiceIndLabCostCalculated;
empsupp is object of a class and the properties listed above are type of bool.
Now I have to return an overall status on basis of this calculation:-------------
if any 3 of the last 5 (note: not all property,just take last 5 ) property value found true,then over all status will be true,otherwise false.
I would have done it by simple if else,but I think that is not proper way because if I have to check 100 properties,then?
解决方案
Please check
public int CountBooleanValue(object data) { int count = 0; foreach (PropertyInfo propertyInfo in data.GetType().GetProperties().Skip(5)) { if (propertyInfo.PropertyType == typeof(bool)) { bool value = Convert.ToBoolean(propertyInfo.GetValue(data, null)); if (value == true) { count++; } } } return count; }
这篇关于循环遍历属性和计数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文