patterncsharpMajor
Generic Null/Empty check for each property of a class
Viewed 0 times
genericeachnullemptypropertyforcheckclass
Problem
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
///
/// The instance of the class
/// Result of the evaluation
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))
{
```
/// To check the properties of a class for Null/Empty values
///
/// The instance of the class
/// Result of the evaluation
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))
{
Solution
First of all I'd reduce indentation. It makes your code really too hard to read. Doing that you will see it may be simplified.
First of all
This code:
May be replaced simply with:
You do not need to declare a local variable for
You do not have to check condition
You do not need to use to
Let's see how to fill this function. Let's read the value of the property:
First of all a common case, valid for all reference types:
Then explicitly check for strings:
For
What left in your original function? In this example let me use simplified check (negative numbers are not empty and I do not check for
Last note: if working with databases you may also want to check equality with
First of all
try/catch: a catch-all block where you rethrow the exception is useless (and you may even throw away line number information.)This code:
if (obj != null)
{
// Do something...
}May be replaced simply with:
if (obj == null)
return false;You do not need to declare a local variable for
result, simply return its value where appropriate.You do not have to check condition
result == false inside your foreach loop (you will still enumerate through all properties), simply break the loop and exit immediately.You do not need to use to
dynamic, when casting you have the right type at compile-time. Also move all this code inside a separate function:private static bool IsNullOrEmpty(object obj) {
}Let's see how to fill this function. Let's read the value of the property:
object value = pi.GetValue(obj);First of all a common case, valid for all reference types:
if (Object.ReferenceEquals(value, null))
return true;Then explicitly check for strings:
if (value is string && String.IsNullOrEmpty((string)value))
return true;For
Int32 (note that in your original code value and or other types. With the exception of strings and negative integer numbers you can also do something like this:if (Object.ReferenceEquals(value, null))
return true;
var type = value.GetType();
return type.IsValueType
&& Object.Equals(value, Activator.CreateInstance(type));What left in your original function? In this example let me use simplified check (negative numbers are not empty and I do not check for
String.Empty against strings however it handles nullable types), feel free to pick the one works for you:public static bool IsAnyNullOrEmpty(object obj)
{
if (Object.ReferenceEquals(obj, null))
return true;
return obj.GetType().GetProperties()
.Any(x => IsNullOrEmpty(x.GetValue(obj)));
}
private static bool IsNullOrEmpty(object value)
{
if (Object.ReferenceEquals(value, null))
return true;
var type = value.GetType();
return type.IsValueType
&& Object.Equals(value, Activator.CreateInstance(type));
}Last note: if working with databases you may also want to check equality with
DBNull.Value.Code Snippets
if (obj != null)
{
// Do something...
}if (obj == null)
return false;private static bool IsNullOrEmpty(object obj) {
}object value = pi.GetValue(obj);if (Object.ReferenceEquals(value, null))
return true;Context
StackExchange Code Review Q#147856, answer score: 23
Revisions (0)
No revisions yet.