Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check all properties of an object whether null or empty?

Tags:

c#

properties

I have an object lets call it ObjectA

and that object has 10 properties and those are all strings.

 var myObject = new {Property1="",Property2="",Property3="",Property4="",...} 

is there anyway to check to see whether all these properties are null or empty?

So any built-in method that would return true or false?

If any single of them is not null or empty then the return would be false. If all of them are empty it should return true.

The idea is I do not want to write 10 if statement to control if those properties are empty or null.

Thanks

like image 304
akd Avatar asked Mar 27 '14 09:03

akd


People also ask

How do you check if all keys of an object are empty?

keys() is a static method that returns an Array when we pass an object to it, which contains the property names (keys) belonging to that object. We can check whether the length of this array is 0 or higher - denoting whether any keys are present or not. If no keys are present, the object is empty: Object.


2 Answers

You can do it using Reflection

bool IsAnyNullOrEmpty(object myObject) {     foreach(PropertyInfo pi in myObject.GetType().GetProperties())     {         if(pi.PropertyType == typeof(string))         {             string value = (string)pi.GetValue(myObject);             if(string.IsNullOrEmpty(value))             {                 return true;             }         }     }     return false; } 

Matthew Watson suggested an alternative using LINQ:

return myObject.GetType().GetProperties()     .Where(pi => pi.PropertyType == typeof(string))     .Select(pi => (string)pi.GetValue(myObject))     .Any(value => string.IsNullOrEmpty(value)); 
like image 175
helb Avatar answered Sep 17 '22 15:09

helb


I suppose you want to make sure that all properties are filled in.

A better option is probably by putting this validation in the constructor of your class and throw exceptions if validation fails. That way you cannot create a class that is invalid; catch exceptions and handle them accordingly.

Fluent validation is a nice framework (http://fluentvalidation.codeplex.com) for doing the validation. Example:

public class CustomerValidator: AbstractValidator<Customer>  {     public CustomerValidator()     {         RuleFor(customer => customer.Property1).NotNull();         RuleFor(customer => customer.Property2).NotNull();         RuleFor(customer => customer.Property3).NotNull();     } }  public class Customer {     public Customer(string property1, string property2, string property3)     {          Property1  = property1;          Property2  = property2;          Property3  = property3;          new CustomerValidator().ValidateAndThrow();     }      public string Property1 {get; set;}     public string Property2 {get; set;}     public string Property3 {get; set;} } 

Usage:

 try  {      var customer = new Customer("string1", "string", null);      // logic here  } catch (ValidationException ex)  {      // A validation error occured  } 

PS - Using reflection for this kind of thing just makes your code harder to read. Using validation as shown above makes it explicitly clear what your rules are; and you can easily extend them with other rules.

like image 45
L-Four Avatar answered Sep 20 '22 15:09

L-Four