[英]C# Loop/Iterate through object to get property values with complex property types
我試圖找到一種方法來循環遍歷一個對象以獲取對象的所有屬性(它們的名稱和它們的值)。 我可以成功地遍歷簡單的屬性(例如字符串、整數等...,但是當它有一個包含屬性的屬性時——這就是問題所在......
[ Working for Simple string/int/bool properties ], but I need something that will work with nested / complex property types.
foreach (PropertyInfo spotProperties in spot.GetType().GetProperties())
{
// Simple property type (string, int, etc...) add the property and its value to the node.
var attributeName = spotProperties.Name;
resultElement.Add(new XElement(attributeName, spotProperties.GetValue(spot, null)));
}
我正在嘗試完成但無法開始工作的示例代碼 // 無法通過復雜的屬性類型循環工作。
foreach (PropertyInfo spotProperties in spot.GetType().GetProperties())
{
if (--spotProperties is complex type then --)
{
// The item is a complex data type, and needs to have it's properties iterated and added to the node.
foreach (PropertyInfo childSpotProperty in spotProperties.GetValue(spot, null).GetType().GetProperties())
{
var attributeName = ((DisplayNameAttribute)childSpotProperty.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute)?.DisplayName ?? childSpotProperty.Name;
//resultElement.Add(new XElement(attributeName, childSpotProperty.GetValue(childSpotProperty, null)));
}
}
else
{
// Simple property type (string, int, etc...) add the property and its value to the node.
var attributeName = spotProperties.Name;
resultElement.Add(new XElement(attributeName, spotProperties.GetValue(spot, null
}
}
如果有人有任何想法,請告訴我。 謝謝,我感謝任何反饋。
您可以根據自己的喜好重構它,但它應該可以完成基本工作。 它使用一些遞歸來遍歷復雜對象中的所有屬性。 它還處理可枚舉的屬性。
public class PropertyInformation
{
public string Name { get; set; }
public object Value { get; set; }
}
public static List<PropertyInformation> ObjectPropertyInformation(object obj)
{
var propertyInformations = new List<PropertyInformation>();
foreach (var property in obj.GetType().GetProperties())
{
//for value types
if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType == typeof(string))
{
propertyInformations.Add(new PropertyInformation { Name = property.Name, Value = property.GetValue(obj) });
}
//for complex types
else if (property.PropertyType.IsClass && !typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
propertyInformations.AddRange(ObjectPropertyInformation(property.GetValue(obj)));
}
//for Enumerables
else
{
var enumerablePropObj1 = property.GetValue(obj) as IEnumerable;
if (enumerablePropObj1 == null) continue;
var objList = enumerablePropObj1.GetEnumerator();
while (objList.MoveNext())
{
objList.MoveNext();
ObjectPropertyInformation(objList.Current);
}
}
}
return propertyInformations;
}
這就像一個冠軍,但它確實有一個錯誤(也許為什么不投票?)
修復如下圖所示:
//for Enumerables
else
{
var enumerablePropObj1 = property.GetValue(obj) as IEnumerable;
if (enumerablePropObj1 == null) continue;
var objList = enumerablePropObj1.GetEnumerator();
while (objList.MoveNext())
{
== if(objList.Current != null)
== {
== propertyInformations.AddRange(ObjectPropertyInformation(objList.Current));
== }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.