System Object extensions for easier conditionals (Extending Base Object Types)


/ Published in: C#
Save to your folder(s)

.net 3.0+, To create stability in your code, you must check all inputs into a method to make sure they are valid . Often people overlook this step because of laziness, or because they aren't convinced that exceptions and assertions are actually in place to help you instead of cause you a headache. Here are some extensions which can be used to enhance the System.Object class so that their methods are global to all classes; this makes input checking effortless. This is also somewhat generic in nature because the extensions take no class type in to account before they are called, but we can use type checking to perform different boxing (casting) operations. Sadly this cannot be done in VB.net because late binding restrictions do not allow the Object class to be extended (See my VB.Net example for further information).


Copy this code and paste it in your HTML
  1. /*
  2. Start by creating a system namespace and class, I used the name "Extensions" but any name can be used. The
  3. keyword for creating extensions is the "this" operator in the method variable declaration. I also use "static"
  4. methods so they can be used on uninstantiated objects.
  5. */
  6.  
  7. namespace System
  8. {
  9.  
  10. public static partial class Extensions
  11. {
  12. public static bool IsNullType(this object obj)
  13. {
  14. return (obj == null || obj is DBNull);
  15. }
  16.  
  17. public static bool IsSigned(this object obj)
  18. {
  19. return (obj is ValueType && (obj is Int32 || obj is Int64 || obj is Int16 || obj is IntPtr || obj is decimal || obj is SByte));
  20. }
  21.  
  22. public static bool IsEmpty(this object obj)
  23. {
  24. return (!IsNullType(obj) && (
  25. (obj is String && ((string)obj).Length == 0) ||
  26. (obj is StringBuilder && ((StringBuilder)obj).Length == 0) ||
  27. (obj is ICollection && ((ICollection)obj).Count == 0) ||
  28. (obj is Array && ((Array)obj).Length == 0) ||
  29. (IsSigned(obj) && obj == (ValueType)(-1)) ||
  30. (obj is ValueType && obj == (ValueType)(0)) ||
  31. (obj is Guid && ((Guid)obj) == Guid.Empty)
  32. ));
  33. }
  34.  
  35. public static bool IsNullTypeOrEmpty(this object obj)
  36. {
  37. return (IsNullType(obj) || IsEmpty(obj));
  38. }
  39.  
  40. }
  41. }
  42.  
  43. /*
  44. An example of how this makes your life easier is checking to see if a string is DBNull or Null or Empty all
  45. in one shot in an efficient manner without having to type the long conditional over and over:
  46. */
  47.  
  48. private void Test(string testVar)
  49. {
  50. if (testVar.IsNullTypeOrEmpty())
  51. throw new Exception("Invalid testVar");
  52.  
  53. DoSomething(testVar);
  54. }
  55.  
  56. /*
  57. As a further note: you could add interface checking to the IsEmpty extension which checks the "emptiness" of
  58. a class you have created if it is not derived from a type that has a way to check this outright. One .net
  59. example if this is the Guid class which has an Empty() method.
  60. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.