获取子对象列表的 属性 个值

Getting property values of list of child objects

假设我有一个 Car class 具有一些属性。其中一个属性是另一个 class、PassengerIEnumerable,具有自己的属性。

public class Car
{
   public string Engine { get; set; }
   public int Wheels { get; set; }
   public IEnumerable<Passenger> Passengers { get; set }
}

我能够获取 Engine 和 Wheels 属性(以及所有其他此类属性)的值。但是,我不知道如何获取所有 Passenger 对象的属性。这是我为获取 Car 对象的 属性 值所做的操作:

Type type = car.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
   object carValue = property.GetValue(car, null); 
   Console.WriteLine(carValue.ToString());
}

这显然没有给我 Passenger 属性,而是输出如下内容:

System.Collections.GenericList'1[Passenger]

如何获取 car.Passengers 列表中每个 Passenger 的所有 属性 值?

您可以混合使用 Type.IsGenericType and Type.GetGenericTypeDefinition()

private void DisplayObject(object obj)
{
    var type = obj.GetType();
    foreach(var propertyInfo in type.GetProperties())
    {
        object value = propertyInfo.GetValue(obj, null);
        if(propertyInfo.PropertyType.IsGenericType && 
            propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        {
            foreach(object o in (IEnumerable)value)
            {
                DisplayObject(o);
            }
        }
        else
        {
            Console.WriteLine(value);
        }
    }
}

你必须首先检查 Car 的 属性 是否是 IEnumerable,如果是,那么你将不得不再次对其使用反射。这可以更好地使用递归来完成,如下所示:

您的汽车对象:

public class Car
    {
        public string Engine { get; set; }
        public int Wheels { get; set; }
        public IEnumerable<Passenger> Passengers { get; set; }
    }

示例应用程序:

    internal class Program
    {
        private static void Main(string[] args)
        {
            var car = new Car
            {
                Engine = "1234",
                Wheels = 4,
                Passengers = new List<Passenger>
                {
                    new Passenger
                    {
                        Id = 1,
                        Name = "Bhushan"
                    }
                }
            };

            ReflectObject(car);
            Console.ReadKey();
        }

使用的函数:

        private static void ReflectObject(object objectToReflect)
        {
            foreach (var propertyInfo in objectToReflect.GetType().GetProperties())
            {
                object propertyValue = propertyInfo.GetValue(objectToReflect, null);
                if (IsEnumerable(propertyInfo))
                {
                    foreach (object obj in (IEnumerable)propertyValue)
                    {
                        ReflectObject(obj);
                    }
                }
                else
                {
                    Console.WriteLine(propertyValue);
                }
            }
        }

        private static bool IsEnumerable(PropertyInfo propertyInfo)
        {
            return propertyInfo.PropertyType.IsGenericType &&
                     propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
        }
    }

输出:

1234

4

1

布山