【问题标题】:How to find values of nested object properties如何查找嵌套对象属性的值
【发布时间】:2017-03-02 21:53:37
【问题描述】:

我有一个函数可以在对象上找到具有空值的属性并将它们设置为 null。这工作正常。现在的问题是有时有一个嵌套对象,我不知道如何迭代它来执行相同的逻辑。

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

例如,假设我有一个 Customer 对象,并且在该对象上我有一个 Address 对象。我现在拥有的函数将查找 Customer 对象上的所有空值并将它们转换为 null,但它还需要查找嵌套 Address 对象上的所有值并将它们转换为 null。可以为不同的对象类型调用此函数,并且并非所有对象类型都有嵌套对象。有什么想法吗?

更新:

所以这行得通,但我真的很想在不必指定对象类型 AddressDto 的情况下完成此操作。我希望它是动态的并接受任何对象类型。

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

【问题讨论】:

  • 为什么要设置一个对象的属性,然后设置为空?有什么意义?
  • 我最初没有设置它们。我正在从另一个进程接收它们,它们可能是空的。

标签: c# object reflection


【解决方案1】:

首先,假设'type'参数是请求参数的类型,你不需要传递它。 typeof(T) 会给你类型。

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

接下来,判断属性是否为类,可以调用

value.GetType().IsClass

所以您的代码可能如下所示:

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

【讨论】:

  • 不会T 解决递归调用的对象吗?不应该改成根本不使用泛型,用request.GetType()代替typeof(T)吗?
  • 蒂姆,你是对的。这种方法只有在我使用request.GetType() 时才有效。 typeof(value).IsClass 也不起作用,原因有两个。一个是,typeof 不会接受变量作为类型,因此它甚至不会编译。如果我将其更改为value.GetType().IsClass,它仍然无法工作,因为我的一些属性是字符串,而字符串是类的类型......
  • 对,我说 value.GetType() 然后输入 typeof(value)。我的错。固定的。是的,您可以使用 request.GetType() 而不是 typeof(T)。无论哪种方式,您都不需要显式传递类型。
【解决方案2】:

根据 Mark Saphiro 的回答,我稍微更改了代码以避免引用循环。我将访问的对象保存在 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;
}

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2022-11-16
    • 2021-11-14
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多