简体   繁体   English

如何查找嵌套对象属性的值

[英]How to find values of nested object properties

I have a function that will find properties with empty values on an object and set them to null. 我有一个函数,该函数将在对象上查找具有空值的属性并将其设置为null。 This works fine. 这很好。 The problem now is that sometimes there is a nested object and I'm not sure how to iterate through that to do the same logic. 现在的问题是,有时会有一个嵌套的对象,我不确定如何遍历该对象以执行相同的逻辑。

public static T SetEmptyPropertiesNull<T>(T request, Type type)
{
    foreach (PropertyInfo property in type.GetProperties())
    {
        object value = property.GetValue(request, null);

        if (string.IsNullOrWhiteSpace((value ?? string.Empty).ToString()))
            property.SetValue(request, null);
    }

    return request;
}

So for example, say I have a Customer object and on that object I have an Address object. 例如,假设我有一个Customer对象,在该对象上有一个Address对象。 The function I have now will find all of the empty values on the Customer object and convert them to null, but it also needs to find all of the values on the nested Address object and convert them to null. 我现在拥有的函数将在Customer对象上找到所有空值并将其转换为null,但它还需要在嵌套的Address对象上找到所有值并将其转换为null。 This function can be called for different object types and not all object types will have a nested object. 可以为不同的对象类型调用此函数,并且并非所有对象类型都具有嵌套对象。 Any ideas? 有任何想法吗?

UPDATE: 更新:

So this works, but I would really like to accomplish this without having to specify the object type, AddressDto. 这样就行了,但是我真的很想完成此操作而不必指定对象类型AddressDto。 I would like it to be dynamic and accept any object type. 我希望它是动态的并接受任何对象类型。

public static T SetEmptyPropertiesNull<T>(T request)
{
    foreach (PropertyInfo property in request.GetType().GetProperties())
    {
        object value = property.GetValue(request, null);

        if (value.GetType() == typeof(AddressDto))
            SetEmptyPropertiesNull(value);
        else if (string.IsNullOrWhiteSpace((value ?? string.Empty).ToString()))
            property.SetValue(request, null);
    }

    return request;
}

First off, assuming the 'type' parameter is the type of the request parameter, you don't need to pass it. 首先,假设“ type”参数是请求参数的类型,则无需传递它。 typeof(T) will give you the type. typeof(T)将为您提供类型。

foreach (PropertyInfo property in typeof(T).GetProperties())

Next up, to determine if the property is a class, you can call 接下来,确定该属性是否是一个类,可以调用

value.GetType().IsClass

So your code might look like this: 因此您的代码可能如下所示:

public static T SetEmptyPropertiesNull<T>(T request)
{
    foreach (PropertyInfo property in typeof(T).GetProperties())
    {
        object value = property.GetValue(request, null);

        if (value.GetType().IsClass)
            SetEmptyPropertiesNull(value);
        else if (string.IsNullOrWhiteSpace((value ?? string.Empty).ToString()))
            property.SetValue(request, null);
    }

    return request;
}

Based on Mark Saphiro answer I changed a litle bit the code to avoid reference cycles. 根据Mark Saphiro的回答,我更改了代码的位数以避免引用周期。 I save the visited objects in a HashSet and only recurse if an object is a class and it's not in the set. 我将访问的对象保存在HashSet并且仅在对象是类且不在集合中时才递归。

HashSet<object> hashSet = new HashSet<object>();

public static T SetEmptyPropertiesNull<T>(T request)
{
    foreach (PropertyInfo property in typeof(T).GetProperties())
    {
        object value = property.GetValue(request, null);

        if (typeof(value).IsClass && !hashSet.Contains(value))
        {
            hashSet.Add(value);
            SetEmptyPropertiesNull(value);
        }
        else if (string.IsNullOrWhiteSpace((value ?? string.Empty).ToString()))
            property.SetValue(request, null);
     }
     return request;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM