Home
Manage Your Code
Snippet: System Object extensions for easier conditionals (Extending Base Object Types) (C#)
Title: System Object extensions for easier conditionals (Extending Base Object Types) Language: C#
Description: Extensions example: Here are some extensions which can be used to enhance the System.Object class so that their methods are global to all classes. Views: 353
Author: Bryan Lyman Date Added: 10/14/2011
Copy Code  
1/*
2.net 3.0+, To create stability in your code, you must check all inputs into a method to make sure 
3they are valid. Often people overlook this step because of laziness, or because they aren't 
4convinced that exceptions and assertions are actually in place to help you instead of cause you a 
5headache. Here are some extensions which can be used to enhance the System.Object class so that 
6their methods are global to all classes; this makes input checking effortless. This is also 
7somewhat generic in nature because the extensions take no class type in to account before they are 
8called, but we can use type checking to perform different boxing (casting) operations. 
9
10Sadly this cannot be done in VB.net because late binding restrictions do not allow the Object 
11class to be extended. You can however make them global shared functions which take an object as a 
12parameter (in a module perhaps).
13
14Start by creating a system namespace and class, I used the name "Extensions" but any name can be 
15used. The keyword for creating extensions is the "this" operator in the method variable 
16declaration. I also use "static" methods so they can be used on uninstantiated objects.
17*/
18
19namespace System
20{
21 
22   public static partial class Extensions
23   {
24      public static bool IsNullType(this object obj)
25      {         
26         return (obj == null || obj is DBNull);      
27      }
28 
29      public static bool IsSigned(this object obj)
30      {
31         return (obj is ValueType && (obj is Int32 || obj is Int64 || obj is Int16 || obj is IntPtr || obj is decimal || obj is SByte));
32      }
33 
34      public static bool IsEmpty(this object obj)
35      {
36         return (!IsNullType(obj) && (
37            (obj is String && ((string)obj).Length == 0) ||
38            (obj is StringBuilder && ((StringBuilder)obj).Length == 0) ||  
39            (obj is ICollection && ((ICollection)obj).Count == 0) ||
40            (obj is Array && ((Array)obj).Length == 0) ||
41            (IsSigned(obj) && obj == (ValueType)(-1)) ||
42            (obj is ValueType && obj == (ValueType)(0)) ||
43            (obj is Guid && ((Guid)obj) == Guid.Empty)
44         ));
45      }
46 
47      public static bool IsNullTypeOrEmpty(this object obj)
48      {
49         return (IsNullType(obj) || IsEmpty(obj));
50      }
51 
52   }
53}
Usage
/*an example of how this makes your life easier is checking to see if a string 
is DBNull or Null or Empty all in one shot in an efficient manner without 
having to type the long conditional over and over*/
 
private void Test(string testVar)
{
   if (testVar.IsNullTypeOrEmpty())
      throw new Exception("Invalid testVar");
 
   DoSomething(testVar);
}
Notes
As a further note: you could add interface checking to the IsEmpty extension which checks the "emptiness" of a class you have created if it is not derived from a type that has a way to check this outright. One .net example if this is the Guid class which has an Empty() method.