【问题标题】:Display full name of all properties of an object显示对象所有属性的全名
【发布时间】: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


    【解决方案1】:

    将您的 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);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      一个好的方法是在每次调用 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 + ".");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-18
        • 1970-01-01
        • 2013-10-31
        • 2018-06-28
        • 1970-01-01
        • 2013-04-20
        • 2015-05-17
        • 2021-03-24
        相关资源
        最近更新 更多