【发布时间】:2016-09-18 03:50:49
【问题描述】:
我有两节课:
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Date { get; set; }
public bool isActif { get; set; }
public Quantity Quantity { get; set; }
}
public class Quantity
{
public string Color { get; set; }
public int Number { get; set; }
}
还有一种方法通过反射和递归显示我的客户类的一个实例的所有属性:
public static void DisplayProperties(object objectA)
{
if (objectA != null)
{
Type objectType;
objectType = objectA.GetType();
foreach (PropertyInfo propertyInfo in objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanRead))
{
object valueA = propertyInfo.GetValue(objectA, null);
if (typeof(IComparable).IsAssignableFrom(propertyInfo.PropertyType) || propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType.IsValueType)
{
Console.WriteLine(propertyInfo.ReflectedType.Name + "." + propertyInfo.Name);
}
else
DisplayProperties(valueA);
}
}
}
结果:
Customer.FirstName
Customer.LastName
Customer.Date
Customer.isActif
Quantity.Color
Quantity.Number
但是我想恢复这样的属性的全名
Customer.FirstName
Customer.LastName
Customer.Date
Customer.isActif
Customer.Quantity.Color
Customer.Quantity.Number
怎么办?
【问题讨论】:
标签: c# recursion reflection