【问题标题】:How to get all names and values of any object using reflection and recursion如何使用反射和递归获取任何对象的所有名称和值
【发布时间】:2014-11-03 10:22:48
【问题描述】:

我正在尝试从对象的实例中获取属性名称和值。我需要它来处理包含嵌套对象的对象,在这些对象中我可以简单地传入父实例。

例如,如果我有:

public class ParentObject
{
    public string ParentName { get; set; }
    public NestedObject Nested { get; set; }
}

public class NestedObject
{
    public string NestedName { get; set; }
}

 // in main
 var parent = new ParentObject();
 parent.ParentName = "parent";
 parent.Nested = new NestedObject { NestedName = "nested" };                                   

 PrintProperties(parent); 

我尝试了递归方法:

public static void PrintProperties(object obj)
{
     var type = obj.GetType();

     foreach (PropertyInfo p in type.GetProperties())
     {
         Console.WriteLine(p.Name + ":- " + p.GetValue(obj, null));

         if (p.PropertyType.GetProperties().Count() > 0)
         {              
             // what to pass in to recursive method
             PrintProperties();                                       
          }
        }

        Console.ReadKey();
    }

我如何确定该属性是传递给 PrintProperties 的内容?

【问题讨论】:

  • 在调用“GetProperties”之前,您可能应该检查类型(它是一个类吗?)以确保 GetProperties 是相关的。要获取值,请在每个上调用 p.GetValue(obj),并将其与“p”一起传递给“PrintProperty”方法。

标签: c# .net reflection


【解决方案1】:

你已经得到了价值,试试这个:

object propertyValue = p.GetValue(obj, null);
Console.WriteLine(p.Name + ":- " + propertyValue);

if (p.PropertyType.GetProperties().Count() > 0)
{              
    // what to pass in to recursive method
    PrintProperties(propertyValue);
}

【讨论】:

    【解决方案2】:

    要解决此问题,您可以使用此代码稍作修复

        private Dictionary<string, string> GetAllProperties(object objectItem, Dictionary<string, string> result)
        {
            if (objectItem == null || objectItem.GetType().IsPrimitive)
            {
                return result;
            };
    
            try
            {
                Type objType = objectItem.GetType();
                PropertyInfo[] properties = objType.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    object propValue = property.GetValue(objectItem, null);
                    if (propValue == null)
                    {
                        continue;
                    }
                    if (propValue is IList subPropValues)
                    {
                        foreach (var item in subPropValues)
                        {
                            GetAllProperties(item, result);
                        }
                    }
                    else
                    {
                        // This will not cut-off System.Collections because of the first check
                        if (property.PropertyType.Assembly == objType.Assembly)
                        {
                            GetAllProperties(propValue, result);
                        }
                        else
                        {
                            if (IsValidPrimaryType(propValue))
                            {
                                result.Add($"{objectItem.GetType().Name}:{property.Name}", propValue.ToString());
                            }
                        }
                    }
                }
            }
            catch
            {
    
                return result;
            }
    
            return result;
        }
    

    使用它

    var myObject = yourObject;
    var myPropsAndValues = new Dictionary<string, string>();
    GetAllProperties(myObject, myPropsAndValues);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多