[英]Getting property values of list of child objects
Let's say I have a Car
class with some properties. 假设我有一个带有某些属性的
Car
类。 One of the properties is an IEnumerable
of another class, Passenger
, with it's own properties. 其中一个属性是另一个类的
IEnumerable
,它具有自己的属性Passenger
。
public class Car
{
public string Engine { get; set; }
public int Wheels { get; set; }
public IEnumerable<Passenger> Passengers { get; set }
}
I am able to get the values of the Engine and Wheels properties (and all others such properties). 我能够获取Engine和Wheels属性的值(以及所有其他此类属性)。 However, I can't figure out how to get the properties of all the
Passenger
objects. 但是,我不知道如何获取所有
Passenger
对象的属性。 Here's what I'm doing to get the property values of a Car
object: 这是我要获取
Car
对象的属性值的操作:
Type type = car.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
object carValue = property.GetValue(car, null);
Console.WriteLine(carValue.ToString());
}
This obviously doesn't give me the Passenger
properties, but outputs something like this instead: 显然,这并没有给我
Passenger
属性,而是输出了以下内容:
System.Collections.GenericList'1[Passenger]
How do I grab all the property values of every Passenger
in the car.Passengers
list? 如何获取
car.Passengers
每位Passenger
所有财产价值? Passenger
列表?
You can do that with a mix of Type.IsGenericType
and Type.GetGenericTypeDefinition()
. 您可以将
Type.IsGenericType
和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);
}
}
}
You will have to check whether the property of Car
is an IEnumerable
first and if it is, then you will have to again use reflection on it. 您将必须首先检查
Car
的属性是否为IEnumerable
,如果是,则必须在其上再次使用反射。 this can better be done using recursion as below: 最好使用以下递归来完成:
Your Car Object: 您的汽车对象:
public class Car
{
public string Engine { get; set; }
public int Wheels { get; set; }
public IEnumerable<Passenger> Passengers { get; set; }
}
Sample Application: 样例应用:
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();
}
Functions Used: 使用的功能:
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<>);
}
}
Output: 输出:
1234 1234
4 4
1 1
Bhushan 布尚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.