如何使用反射和递归获取任何对象的所有名称和值

时间:2014-11-03 10:22:49

标签: c# .net reflection

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

例如,如果我有:

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?

2 个答案:

答案 0 :(得分:2)

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

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);
}

答案 1 :(得分:0)

要解决此问题,您可以使用此代码进行一点修复

    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);