Quantcast
Channel: Generic Null/Empty check for each property of a class - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 7

Generic Null/Empty check for each property of a class

$
0
0

I have created a method to check for null/empty values of class properties and if any null property is found I'm stopping the checking process and returning the result as true. I've used a solution from here and following is the code I have implemented:

/// To check the properties of a class for Null/Empty values/// </summary>/// <param name="obj">The instance of the class</param>/// <returns>Result of the evaluation</returns>public static bool IsAnyNullOrEmpty(object obj){    //Step 1: Set the result variable to false;    bool result = false;    try    {        //Step 2: Check if the incoming object has values or not.        if (obj != null)        {            //Step 3: Iterate over the properties and check for null values based on the type.            foreach (PropertyInfo pi in obj.GetType().GetProperties())            {                //Step 4: The null check condition only works if the value of the result is false, whenever the result gets true, the value is returned from the method.                if (result == false)                {                    //Step 5: Different conditions to satisfy different types                    dynamic value;                    if (pi.PropertyType == typeof(string))                    {                        value = (string)pi.GetValue(obj);                        result = (string.IsNullOrEmpty(value) ? true : false || string.IsNullOrWhiteSpace(value) ? true : false);                    }                    else if (pi.PropertyType == typeof(int))                    {                        value = (int)pi.GetValue(obj);                        result = (value <= 0 ? true : false || value == null ? true : false);                    }                    else if (pi.PropertyType == typeof(bool))                    {                        value = pi.GetValue(obj);                        result = (value == null ? true : false);                    }                    else if (pi.PropertyType == typeof(Guid))                    {                        value = pi.GetValue(obj);                        result = (value == Guid.Empty ? true : false || value == null ? true : false);                    }                }                //Step 6 - If the result becomes true, the value is returned from the method.                else                    return result;            }        }    }    catch (Exception ex)    {        throw ex;    }    //Step 7: If the value doesn't become true at the end of foreach loop, the value is returned.    return result;}

I want to extend this code to another end that instead of sending the bool result as true or false, I'll clone the incoming object and based on the values inside the properties can send the object.

For example:

Let's say this is my model: Order.cs

public class Order{    [DataMember]    public Guid OrderId { get; set; }    [DataMember]    public string Owner { get; set; }    [DataMember]    public string Info { get; set; }    [DataMember]    public string Recipient { get; set; }    [DataMember]    public int Test { get; set; }    [DataMember]    public DateTime CreatedOn { get; set; }}

Now if there is any null or empty value in any of the properties, I'll be returning a new class which contains only those values which are either null and empty and the status of it as:

//This class is the output class from the IsAnyNullOrEmpty() method and has only those properties which are either null or emptypublic class ProcessedModel{           public string Property1 { get; set; }   //has null value in type String (set property vaule to "Null value")    public string Property2 { get; set; } //has empty value in type String (set property vaule to "Empty value")    public string Property3 { get; set; } //has 0 value in type int (set property vaule to "0 value")}

Am I following the right approach?


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images