检查 Object 在每个属性中是否为 null [英] Checking if Object has null in every property

查看:69
本文介绍了检查 Object 在每个属性中是否为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个属性的类;

public class Employee
{
    public string TYPE { get; set; }
    public int? SOURCE_ID { get; set; }
    public string FIRST_NAME { get; set; }        
    public string LAST_NAME { get; set; }

    public List<Department> departmentList { get; set; }
    public List<Address> addressList { get; set; }

}

有时这个对象会在任何属性中返回我的值

sometimes this object return me with value in any property say

Employee emp = new Employee();
emp.FIRST_NAME= 'abc';

其余值为空.没问题

但是,如何检查对象属性中的所有值何时为空

But, How do I check when all values in properties of objects are null

喜欢 string.IsNullOrEmpty() 对象吗?

目前我是这样检查的;

if(emp.FIRST_NAME == null && emp.LAST_NAME == null && emp.TYPE == null && emp.departmentList == null ...)

推荐答案

EDIT

这个答案在上次得到了一些投票,所以我决定稍微改进它,添加简单的缓存,以便 ArePropertiesNotNull 不会在每次调用时检索属性,而只是每种类型一次.

EDIT

This answer has received some votes in the last time, so I decided to improve it a little, adding simple caching so that ArePropertiesNotNull does not retrieve the properties every time it is called, but rather only once for every type.

public static class PropertyCache<T>
{
    private static readonly Lazy<IReadOnlyCollection<PropertyInfo>> publicPropertiesLazy
        = new Lazy<IReadOnlyCollection<PropertyInfo>>(() => typeof(T).GetProperties());

    public static IReadOnlyCollection<PropertyInfo> PublicProperties => PropertyCache<T>.publicPropertiesLazy.Value;
}

public static class Extensions
{
    public static bool ArePropertiesNotNull<T>(this T obj)
    {
        return PropertyCache<T>.PublicProperties.All(propertyInfo => propertyInfo.GetValue(obj) != null);
    }
}

(下面的旧答案.)

您可以使用 Joel Harkes 提出的反射,例如我把这个可重用、随时可用的扩展方法组合在一起

You could use reflection as proposed by Joel Harkes, e.g. I put together this reusable, ready-to-use extension method

public static bool ArePropertiesNotNull<T>(this T obj)
{
    return typeof(T).GetProperties().All(propertyInfo => propertyInfo.GetValue(obj) != null);    
}

然后可以这样调用

var employee = new Employee();
bool areAllPropertiesNotNull = employee.ArePropertiesNotNull();

现在您可以检查 areAllPropertiesNotNull 标志,该标志指示是否所有属性都不为空.如果所有属性都不为空,则返回 true,否则返回 false.

And now you can check the areAllPropertiesNotNull flag which indicates whether all properties are not null. Returns true if all properties are not null, otherwise false.

  • 对于检查来说,属性类型是否可以为空并不重要.
  • 由于上述方法是通用的,您可以将其用于您想要的任何类型,而不必为您想要检查的每种类型编写样板代码.
  • 如果您以后更改课程,它会更加面向未来.(由 ispiro 注明).
  • 反射可能非常慢,在这种情况下,它肯定比您目前编写的显式代码慢.使用简单的缓存(如 Reginald Blue 所提议的那样)将消除大部分开销.
  • Reflection can be quite slow, and in this case it is certainly slower than writing explicit code as you currently do. Using simple caching (as proposed by Reginald Blue will remove much of that overhead.

在我看来,由于使用ArePropertiesNotNull 时开发时间和代码重复减少了,所以轻微的性能开销可以忽略不计,但是YMMV.

In my opinion, the slight performance overhead can be neglected since development time and repetition of code are reduced when using the ArePropertiesNotNull, but YMMV.

这篇关于检查 Object 在每个属性中是否为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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