Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// 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;
- }
- 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; }
- }
- //This class is the output class from the IsAnyNullOrEmpty() method and has only those properties which are either null or empty
- public 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")
- }
- 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;
- if (value is string && String.IsNullOrEmpty((string)value))
- return true;
- if (value is int)
- return ((int)value) <= 0;
- if (value is Guid)
- return ((Guid)value) == Guid.Empty;
- if (Object.ReferenceEquals(value, null))
- return false;
- var type = value.GetType();
- return type.IsValueType
- && Object.Equals(value, Activator.CreateInstance(type));
- public static bool IsAnyNullOrEmpty(object obj)
- {
- if (Object.ReferenceEquals(obj, null))
- return false;
- return obj.GetType().GetProperties()
- .Any(x => IsNullOrEmpty(x.GetValue(obj)));
- }
- private static bool IsNullOrEmpty(object value)
- {
- if (Object.ReferenceEquals(value, null))
- return false;
- var type = value.GetType();
- return type.IsValueType
- && Object.Equals(value, Activator.CreateInstance(type));
- }
- public static bool AllPropertiesValid(object obj)
- {
- return !obj.GetType().GetProperties().All(p =>
- {
- var value = p.GetValue(obj);
- if (value == null) { return false; }
- if (p.PropertyType == typeof(string))
- {
- return string.IsNullOrEmpty((string)value);
- }
- if (p.PropertyType == typeof(int))
- {
- return ((int)value <= 0);
- }
- if (p.PropertyType == typeof(bool))
- {
- return (!(bool)value);
- }
- if (p.PropertyType == typeof(Guid))
- {
- return ((Guid)value) == Guid.Empty;
- }
- return true;
- });
- }
- public class SampleClass
- {
- public int Property1 { get; set; }
- public bool Property2 { get; set; }
- public Guid Property3 { get; set; }
- public string Property4 { get; set; }
- public object Property5 { get; set; }
- }
- [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
- public abstract class ValidatorAttribute : Attribute
- {
- public abstract bool Validate(object value);
- }
- public class DefaultValidatorAttribute : ValidatorAttribute
- {
- public override bool Validate(object value)
- {
- if (value is bool)
- return (bool)value != false;
- if (value is string)
- return !string.IsNullOrWhiteSpace((string)value);
- if (value.GetType().IsValueType && value.GetType().IsPrimitive)
- return (double)value != 0.0;
- return value != null;
- }
- }
- public class IntValidatorAttribute : ValidatorAttribute
- {
- public override bool Validate(object value)
- {
- return (int)value <= 0;
- }
- }
- public class GuidValidatorAttribute : ValidatorAttribute
- {
- public override bool Validate(object value)
- {
- if (!(value is Guid))
- return false;
- return ((Guid)value) != Guid.Empty;
- }
- }
- public class SampleClass
- {
- [IntValidator]
- public int Property1 { get; set; }
- public bool Property2 { get; set; }
- [GuidValidator]
- public Guid Property3 { get; set; }
- public string Property4 { get; set; }
- public object Property5 { get; set; }
- }
- public static class ValidationExtensions
- {
- public static bool AllPropertiesValid(this object obj)
- {
- if (obj == null)
- return false;
- return obj.GetType().GetProperties().All(p =>
- {
- var attrib = p.GetCustomAttributes(typeof(ValidatorAttribute), true)
- .FirstOrDefault() as ValidatorAttribute;
- if (attrib == null)
- attrib = new DefaultValidatorAttribute();
- return attrib.Validate(p.GetValue(obj));
- });
- }
- }
- public class SkipValidator : ValidatorAttribute
- {
- public override bool Validate(object value)
- {
- return true;
- }
- }
- public class SampleClass
- {
- [SkipValidator]
- public int DoNotCare { get; set; }
- }
Add Comment
Please, Sign In to add comment