Guest User

Untitled

a guest
Jun 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. /// To check the properties of a class for Null/Empty values
  2. /// </summary>
  3. /// <param name="obj">The instance of the class</param>
  4. /// <returns>Result of the evaluation</returns>
  5. public static bool IsAnyNullOrEmpty(object obj)
  6. {
  7. //Step 1: Set the result variable to false;
  8. bool result = false;
  9.  
  10. try
  11. {
  12. //Step 2: Check if the incoming object has values or not.
  13. if (obj != null)
  14. {
  15. //Step 3: Iterate over the properties and check for null values based on the type.
  16. foreach (PropertyInfo pi in obj.GetType().GetProperties())
  17. {
  18. //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.
  19. if (result == false)
  20. {
  21. //Step 5: Different conditions to satisfy different types
  22. dynamic value;
  23. if (pi.PropertyType == typeof(string))
  24. {
  25. value = (string)pi.GetValue(obj);
  26. result = (string.IsNullOrEmpty(value) ? true : false || string.IsNullOrWhiteSpace(value) ? true : false);
  27. }
  28. else if (pi.PropertyType == typeof(int))
  29. {
  30. value = (int)pi.GetValue(obj);
  31. result = (value <= 0 ? true : false || value == null ? true : false);
  32. }
  33. else if (pi.PropertyType == typeof(bool))
  34. {
  35. value = pi.GetValue(obj);
  36. result = (value == null ? true : false);
  37. }
  38. else if (pi.PropertyType == typeof(Guid))
  39. {
  40. value = pi.GetValue(obj);
  41. result = (value == Guid.Empty ? true : false || value == null ? true : false);
  42. }
  43. }
  44. //Step 6 - If the result becomes true, the value is returned from the method.
  45. else
  46. return result;
  47. }
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. throw ex;
  53. }
  54.  
  55. //Step 7: If the value doesn't become true at the end of foreach loop, the value is returned.
  56. return result;
  57. }
  58.  
  59. public class Order
  60. {
  61.  
  62. [DataMember]
  63. public Guid OrderId { get; set; }
  64.  
  65. [DataMember]
  66. public string Owner { get; set; }
  67.  
  68. [DataMember]
  69. public string Info { get; set; }
  70.  
  71. [DataMember]
  72. public string Recipient { get; set; }
  73.  
  74. [DataMember]
  75. public int Test { get; set; }
  76.  
  77. [DataMember]
  78. public DateTime CreatedOn { get; set; }
  79. }
  80.  
  81. //This class is the output class from the IsAnyNullOrEmpty() method and has only those properties which are either null or empty
  82. public class ProcessedModel
  83. {
  84. public string Property1 { get; set; } //has null value in type String (set property vaule to "Null value")
  85.  
  86. public string Property2 { get; set; } //has empty value in type String (set property vaule to "Empty value")
  87.  
  88. public string Property3 { get; set; } //has 0 value in type int (set property vaule to "0 value")
  89. }
  90.  
  91. if (obj != null)
  92. {
  93. // Do something...
  94. }
  95.  
  96. if (obj == null)
  97. return false;
  98.  
  99. private static bool IsNullOrEmpty(object obj) {
  100. }
  101.  
  102. object value = pi.GetValue(obj);
  103.  
  104. if (Object.ReferenceEquals(value, null))
  105. return true;
  106.  
  107. if (value is string && String.IsNullOrEmpty((string)value))
  108. return true;
  109.  
  110. if (value is int)
  111. return ((int)value) <= 0;
  112.  
  113. if (value is Guid)
  114. return ((Guid)value) == Guid.Empty;
  115.  
  116. if (Object.ReferenceEquals(value, null))
  117. return false;
  118.  
  119. var type = value.GetType();
  120. return type.IsValueType
  121. && Object.Equals(value, Activator.CreateInstance(type));
  122.  
  123. public static bool IsAnyNullOrEmpty(object obj)
  124. {
  125. if (Object.ReferenceEquals(obj, null))
  126. return false;
  127.  
  128. return obj.GetType().GetProperties()
  129. .Any(x => IsNullOrEmpty(x.GetValue(obj)));
  130. }
  131.  
  132. private static bool IsNullOrEmpty(object value)
  133. {
  134. if (Object.ReferenceEquals(value, null))
  135. return false;
  136.  
  137. var type = value.GetType();
  138. return type.IsValueType
  139. && Object.Equals(value, Activator.CreateInstance(type));
  140. }
  141.  
  142. public static bool AllPropertiesValid(object obj)
  143. {
  144. return !obj.GetType().GetProperties().All(p =>
  145. {
  146. var value = p.GetValue(obj);
  147.  
  148. if (value == null) { return false; }
  149.  
  150. if (p.PropertyType == typeof(string))
  151. {
  152. return string.IsNullOrEmpty((string)value);
  153. }
  154.  
  155. if (p.PropertyType == typeof(int))
  156. {
  157. return ((int)value <= 0);
  158. }
  159.  
  160. if (p.PropertyType == typeof(bool))
  161. {
  162. return (!(bool)value);
  163. }
  164.  
  165. if (p.PropertyType == typeof(Guid))
  166. {
  167. return ((Guid)value) == Guid.Empty;
  168. }
  169.  
  170. return true;
  171. });
  172. }
  173.  
  174. public class SampleClass
  175. {
  176. public int Property1 { get; set; }
  177. public bool Property2 { get; set; }
  178. public Guid Property3 { get; set; }
  179. public string Property4 { get; set; }
  180. public object Property5 { get; set; }
  181. }
  182.  
  183. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
  184. public abstract class ValidatorAttribute : Attribute
  185. {
  186. public abstract bool Validate(object value);
  187. }
  188.  
  189. public class DefaultValidatorAttribute : ValidatorAttribute
  190. {
  191. public override bool Validate(object value)
  192. {
  193. if (value is bool)
  194. return (bool)value != false;
  195.  
  196. if (value is string)
  197. return !string.IsNullOrWhiteSpace((string)value);
  198.  
  199. if (value.GetType().IsValueType && value.GetType().IsPrimitive)
  200. return (double)value != 0.0;
  201.  
  202. return value != null;
  203. }
  204. }
  205.  
  206. public class IntValidatorAttribute : ValidatorAttribute
  207. {
  208. public override bool Validate(object value)
  209. {
  210. return (int)value <= 0;
  211. }
  212. }
  213.  
  214. public class GuidValidatorAttribute : ValidatorAttribute
  215. {
  216. public override bool Validate(object value)
  217. {
  218. if (!(value is Guid))
  219. return false;
  220. return ((Guid)value) != Guid.Empty;
  221. }
  222. }
  223.  
  224. public class SampleClass
  225. {
  226. [IntValidator]
  227. public int Property1 { get; set; }
  228. public bool Property2 { get; set; }
  229. [GuidValidator]
  230. public Guid Property3 { get; set; }
  231. public string Property4 { get; set; }
  232. public object Property5 { get; set; }
  233. }
  234.  
  235. public static class ValidationExtensions
  236. {
  237. public static bool AllPropertiesValid(this object obj)
  238. {
  239. if (obj == null)
  240. return false;
  241.  
  242. return obj.GetType().GetProperties().All(p =>
  243. {
  244. var attrib = p.GetCustomAttributes(typeof(ValidatorAttribute), true)
  245. .FirstOrDefault() as ValidatorAttribute;
  246.  
  247. if (attrib == null)
  248. attrib = new DefaultValidatorAttribute();
  249.  
  250. return attrib.Validate(p.GetValue(obj));
  251. });
  252. }
  253. }
  254.  
  255. public class SkipValidator : ValidatorAttribute
  256. {
  257. public override bool Validate(object value)
  258. {
  259. return true;
  260. }
  261. }
  262.  
  263. public class SampleClass
  264. {
  265. [SkipValidator]
  266. public int DoNotCare { get; set; }
  267. }
Add Comment
Please, Sign In to add comment