C# 如何使用反射和递归获取任何对象的所有名称和值
我试图从对象实例中获取属性名称和值。我需要它来处理包含嵌套对象的对象,我可以在其中简单地传递父实例 例如,如果我有:C# 如何使用反射和递归获取任何对象的所有名称和值,c#,.net,reflection,C#,.net,Reflection,我试图从对象实例中获取属性名称和值。我需要它来处理包含嵌套对象的对象,我可以在其中简单地传递父实例 例如,如果我有: public class ParentObject { public string ParentName { get; set; } public NestedObject Nested { get; set; } } public class NestedObject { public string NestedName { get; set; } }
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的属性?如果已经获得该值,请尝试以下操作:
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);
}
要解决此问题,您可以使用此代码进行少量修复
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;
}
私有字典GetAllProperties(对象对象项、字典结果)
{
if(objectItem==null | | objectItem.GetType().IsPrimitive)
{
返回结果;
};
尝试
{
类型objType=objectItem.GetType();
PropertyInfo[]properties=objType.GetProperties();
foreach(属性中的PropertyInfo属性)
{
object propValue=property.GetValue(objectItem,null);
if(propValue==null)
{
继续;
}
if(propValue为IList SubpValue)
{
foreach(子PV值中的var项)
{
GetAllProperties(项目、结果);
}
}
其他的
{
//这不会因为第一次检查而切断系统收集
if(property.PropertyType.Assembly==objType.Assembly)
{
GetAllProperties(propValue、result);
}
其他的
{
if(IsValidPrimaryType(propValue))
{
Add($“{objectItem.GetType().Name}:{property.Name}”,propValue.ToString());
}
}
}
}
}
抓住
{
返回结果;
}
返回结果;
}
使用它
var myObject = yourObject;
var myPropsAndValues = new Dictionary<string, string>();
GetAllProperties(myObject, myPropsAndValues);
var myObject=yourObject;
var myPropsAndValues=新字典();
GetAllProperties(myObject、MyPropsAndValue);
在调用“GetProperties”之前,您可能应该检查类型(是否为类?),以确保GetProperties是相关的。要获取值,请对每个对象调用p.GetValue(obj),并将其与“p”一起传递给“PrintProperty”方法。