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

如何通过反射遍历List<DetailClass>中的对象(同字符串字段处理方式)

解决反射遍历List对象的问题

我明白你现在的需求:已经能通过反射处理SomeClass里的字符串字段,但不知道怎么处理其中的List<DetailClass>类型属性,遍历里面每个DetailClass对象的字段。下面我给你完整的实现思路和代码示例:

核心思路

  1. 遍历SomeClass的所有属性时,先判断当前属性的类型:
    • 如果是string类型,按你原来的逻辑处理即可;
    • 如果是List<DetailClass>(或者更通用的IEnumerable类型),先获取该属性的实际值(即列表实例),然后遍历列表中的每个DetailClass对象;
  2. 对每个DetailClass对象,再次使用反射获取它的所有属性,像处理SomeClass的字符串字段一样处理这些属性。

完整代码示例

假设你原来的私有静态方法是用来获取类的公共属性,我们可以扩展它来处理嵌套的列表类型:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

class DetailClass 
{ 
    public string FieldDetail1 { get; set; } 
    public string FieldDetail2 { get; set; } 
    public string FieldDetail3 { get; set; } 
} 

class SomeClass 
{ 
    public string Field1 { get; set; } 
    public string Field2 { get; set; } 
    public string Field3 { get; set; } 
    public List<DetailClass> Artikli { get; set; } 
}

class Program
{
    static void Main()
    {
        // 构造测试数据
        var someObj = new SomeClass
        {
            Field1 = "Value1",
            Field2 = "Value2",
            Field3 = "Value3",
            Artikli = new List<DetailClass>
            {
                new DetailClass { FieldDetail1 = "Detail1-1", FieldDetail2 = "Detail1-2", FieldDetail3 = "Detail1-3" },
                new DetailClass { FieldDetail1 = "Detail2-1", FieldDetail2 = "Detail2-2", FieldDetail3 = "Detail2-3" }
            }
        };

        // 遍历处理SomeClass对象的所有属性
        ProcessObjectProperties(someObj);
    }

    // 通用处理对象属性的方法
    private static void ProcessObjectProperties(object obj)
    {
        if (obj == null) return;

        Type objType = obj.GetType();
        PropertyInfo[] properties = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo prop in properties)
        {
            object propValue = prop.GetValue(obj);

            // 处理字符串类型字段
            if (prop.PropertyType == typeof(string))
            {
                Console.WriteLine($"[{objType.Name}] {prop.Name}: {propValue}");
            }
            // 处理IEnumerable类型(排除string,因为string本身也是IEnumerable<char>)
            else if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string))
            {
                Console.WriteLine($"\n[{objType.Name}] 开始遍历列表 {prop.Name}:");
                var enumerable = (IEnumerable)propValue;
                if (enumerable != null)
                {
                    int index = 0;
                    foreach (var item in enumerable)
                    {
                        Console.WriteLine($"  列表元素 {index + 1}:");
                        // 递归处理列表中的每个DetailClass对象
                        ProcessObjectProperties(item);
                        index++;
                    }
                }
            }
            // 可扩展处理其他类型,比如值类型、其他自定义类
            else
            {
                Console.WriteLine($"[{objType.Name}] {prop.Name}: 非字符串/列表类型");
            }
        }
    }
}

代码说明

  1. 递归处理逻辑:ProcessObjectProperties是一个通用方法,能递归处理任意对象的属性——不管是顶层的SomeClass,还是列表里的DetailClass,都能统一处理,不用为每个类单独写逻辑;
  2. 类型判断细节:判断可枚举类型时特意排除了string,因为字符串本身也实现了IEnumerable<char>,避免把字符串当成列表处理;
  3. 灵活性:如果后续你的类结构新增了其他自定义类型属性,只需要在else分支里添加对应的处理逻辑即可,不用大改现有代码。

运行结果

执行代码后会输出:

[SomeClass] Field1: Value1
[SomeClass] Field2: Value2
[SomeClass] Field3: Value3

[SomeClass] 开始遍历列表 Artikli:
  列表元素 1:
[DetailClass] FieldDetail1: Detail1-1
[DetailClass] FieldDetail2: Detail1-2
[DetailClass] FieldDetail3: Detail1-3
  列表元素 2:
[DetailClass] FieldDetail1: Detail2-1
[DetailClass] FieldDetail2: Detail2-2
[DetailClass] FieldDetail3: Detail2-3

这样就完美实现了你想要的效果:既处理SomeClass的字符串字段,又遍历Artikli列表中的每个DetailClass对象,处理它的字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:51:54