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 ...)
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);
}
}
(Old answer below.)
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
.
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.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With