Checking if Object has null in every property

那年仲夏 提交于 2020-01-31 06:57:42

问题


I have class with multiple properties;

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

remaining values are null. This is OK

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

like string.IsNullOrEmpty() for object ?

curretly I am checking like this;

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

回答1:


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

which can then be called like this

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

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.


Advantages of this approach

  • It doesn't matter whether or not the property type is nullable or not for the check.
  • Since above method is generic, you can use it for any type you want and don't have to write boilerplate code for every type you want to check.
  • It is more future-proof in case you change the class later. (noted by ispiro).

Disadvantages

  • 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.

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.




回答2:


Either you do this by writing down the code to check every property manually (best option) or you use reflection (read more here)

Employee emp = new Employee();
var props = emp.GetType().GetProperties())
foreach(var prop in props) 
{
   if(prop.GetValue(foo, null) != null) return false;
}
return true;

example from here

Note that int cannot be null! and its default value will be 0. thus its better to check prop == default(int) than == null

option 3

Another option is to implement INotifyPropertyChanged.

On a change set a boolean field value isDirty to true and than you only need to check if this value is true to know if any property has been set (even if property was set with null.

Warning: this method every property can still be null but only checks if a setter was called (changing a value).



来源:https://stackoverflow.com/questions/50128815/checking-if-object-has-null-in-every-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!