I have following class:-
public class Requirements
{
public string EventMessageUId { get; set; }
public string ProjectId { get; set; }
I use the below extension of the object which I use for validating objects that I don't want to have all properties null or empty in order to save some calls to the database. I think it fits your case along with some more.
///
/// Returns true is all the properties of the object are null, empty or "smaller or equal to" zero (for int and double)
///
/// Any type of object
///
public static bool IsObjectEmpty(this object obj)
{
if (Object.ReferenceEquals(obj, null))
return true;
return obj.GetType().GetProperties()
.All(x => IsNullOrEmpty(x.GetValue(obj)));
}
///
/// Checks if the property value is null, empty or "smaller or equal to" zero (for numeric types)
///
/// The property value
///
private static bool IsNullOrEmpty(object value)
{
if (Object.ReferenceEquals(value, null))
return true;
if (value.GetType().GetTypeInfo().IsClass)
return value.IsObjectEmpty();
if (value is string || value is char || value is short)
return string.IsNullOrEmpty((string) value);
if (value is int)
return ((int) value) <= 0;
if (value is double)
return ((double) value) <= 0;
if (value is decimal)
return ((decimal) value) <= 0;
if (value is DateTime)
return false;
// ........
// Add all other cases that we may need
// ........
if (value is object)
return value.IsObjectEmpty();
var type = value.GetType();
return type.IsValueType
&& Object.Equals(value, Activator.CreateInstance(type));
}
The call is obviously obj.IsObjectEmpty()