C# 显示对象所有属性的全名
我有两门课:C# 显示对象所有属性的全名,c#,recursion,reflection,C#,Recursion,Reflection,我有两门课: 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 cl
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; }
}
还有一个方法,通过反射和递归显示我的类Customer的一个实例的所有属性:
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
如何做?一个好方法是在每次调用
DisplayProperties
时传递路径
更改方法签名:
public static void DisplayProperties(object objectA, string path = "")
改变印刷方法
Console.WriteLine("{0}{1}.{2}", path, propertyInfo.ReflectedType.Name, propertyInfo.Name);
DisplayProperties(valueA, objectA.GetType().Name + ".");
更改递归调用方法
Console.WriteLine("{0}{1}.{2}", path, propertyInfo.ReflectedType.Name, propertyInfo.Name);
DisplayProperties(valueA, objectA.GetType().Name + ".");
一个好的方法是在每次调用
DisplayProperties
时传递路径
更改方法签名:
public static void DisplayProperties(object objectA, string path = "")
改变印刷方法
Console.WriteLine("{0}{1}.{2}", path, propertyInfo.ReflectedType.Name, propertyInfo.Name);
DisplayProperties(valueA, objectA.GetType().Name + ".");
更改递归调用方法
Console.WriteLine("{0}{1}.{2}", path, propertyInfo.ReflectedType.Name, propertyInfo.Name);
DisplayProperties(valueA, objectA.GetType().Name + ".");
将您的
DisplayProperties
方法更改为
public static void DisplayProperties(object objectA, string objName="")
{
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(objName + (objName==""? "" : ".") + propertyInfo.ReflectedType.Name + "." + propertyInfo.Name);
}
else
DisplayProperties(valueA, objName + (objName == "" ? "" : ".") + objectType.Name);
}
}
}
将您的
DisplayProperties
方法更改为
public static void DisplayProperties(object objectA, string objName="")
{
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(objName + (objName==""? "" : ".") + propertyInfo.ReflectedType.Name + "." + propertyInfo.Name);
}
else
DisplayProperties(valueA, objName + (objName == "" ? "" : ".") + objectType.Name);
}
}
}