You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#遍历对象属性计算嵌套类属性填充占比的实现方法

问题背景

首先定义了如下3个C#类:

public class MainObj 
{
    public int BankDetailPercentage { get; set; }
    public int PersonalDetailPercentage { get; set; }
    public BankDetail BankDetail { get; set; }
    public PersonalDetail PersonalDetail { get; set; }
}
public class BankDetail
{
    public string Name { get; set; }
    public string Branch { get; set; }
    public string AccountNumber { get; set; }
}
public class PersonalDetail
{
    public string Name { get; set; }
    public string address { get; set; }
}

需求为:遍历MainObj类的实例,统计其嵌套的BankDetail、PersonalDetail对象中已填充的属性数量,据此计算填充百分比,分别赋值给MainObj实例的BankDetailPercentage、PersonalDetailPercentage属性后返回该实例。
已有未完成的实现代码如下:

public MainObject calculatePercentage(MainObject mainObject) 
{
    int bdCount = 0, pdCount = 0, bdTotal = 3, pdTotal = 2;

    PropertyInfo[] properties = typeof(MainObject).GetProperties();
    foreach (var property in properties)
    {
        var value = property.GetValue(mainObject);
        if (property.Name == "BankDetail")
        {
          // 待实现逻辑
        }
    }
    return mainObject;
}

注:首先需要确认类名统一,你定义的主类名为MainObj,代码中写的是MainObject,需调整一致避免编译错误。

完整实现代码
using System.Reflection;

// 统一主类名为MainObj
public MainObj calculatePercentage(MainObj mainObject) 
{
    int bdCount = 0, pdCount = 0, bdTotal = 3, pdTotal = 2;

    PropertyInfo[] mainProperties = typeof(MainObj).GetProperties();
    foreach (var property in mainProperties)
    {
        var value = property.GetValue(mainObject);
        if (property.Name == "BankDetail" && value != null)
        {
            // 遍历BankDetail所有属性统计非空数量
            PropertyInfo[] bdProperties = typeof(BankDetail).GetProperties();
            foreach (var bdProp in bdProperties)
            {
                var propValue = bdProp.GetValue(value);
                if (propValue != null && !string.IsNullOrWhiteSpace(propValue.ToString()))
                {
                    bdCount++;
                }
            }
        }
        else if (property.Name == "PersonalDetail" && value != null)
        {
            // 遍历PersonalDetail所有属性统计非空数量
            PropertyInfo[] pdProperties = typeof(PersonalDetail).GetProperties();
            foreach (var pdProp in pdProperties)
            {
                var propValue = pdProp.GetValue(value);
                if (propValue != null && !string.IsNullOrWhiteSpace(propValue.ToString()))
                {
                    pdCount++;
                }
            }
        }
    }
    // 计算百分比,结果四舍五入取整
    mainObject.BankDetailPercentage = (int)Math.Round((double)bdCount / bdTotal * 100);
    mainObject.PersonalDetailPercentage = (int)Math.Round((double)pdCount / pdTotal * 100);

    return mainObject;
}
实现说明
  • 如果嵌套的BankDetail或PersonalDetail对象本身为null,对应百分比直接记为0
  • 字符串属性判断了IsNullOrWhiteSpace,空字符串、全空格的属性均视为未填充
  • 百分比计算采用四舍五入取整,可根据需求调整为向下取整等其他逻辑

内容的提问来源于stack exchange,提问作者Luke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 11:15:05