C#循环/遍历对象以获取具有复杂属性类型的属性值 [英] C# Loop/Iterate through object to get property values with complex property types

查看:808
本文介绍了C#循环/遍历对象以获取具有复杂属性类型的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种循环遍历对象的方法,以获取对象的所有属性(其名称和值).我可以成功地迭代简单的属性(例如字符串,int等),但是当它具有包含属性的属性时,这就是问题所在...

I am trying to find a way to loop through and iterate through an object to get all of it's properties (their name and their value) of an object. I can successfully iterate through the simple properties (such as strings, int, etc.., but when it has a property that contains properties - that is where the problem is...

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

我要完成的示例代码,但无法正常工作 //无法遍历复杂的属性类型.

Sample code of what I am trying to accomplish, but could not get to work // Unable to get to work loop through complex property types.

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

请让我知道是否有人有任何想法.谢谢,感谢您的反馈.

Please let me know if anyone has any idea. Thanks, I appreciate any feed back.

推荐答案

您可以根据自己的喜好重构它,但是它应该可以完成基本的工作.它使用一些递归来遍历复杂对象中的所有属性.它还处理可枚举的属性.

You can refactor this to your liking but it should get the basic job done. It uses some recursion to move through all of the properties in the complex objects. It also handles properties that are Enumerable.

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

这篇关于C#循环/遍历对象以获取具有复杂属性类型的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆