Home
Manage Your Code
Snippet: ArgumentHelper (C#)
Title: ArgumentHelper Language: C#
Description: Provides helper methods for asserting arguments. Views: 829
Author: Dave Donaldson Date Added: 5/21/2007
Copy Code  
1public static class ArgumentHelper
2{
3    public static void AssertNull<T>(T paramValue, string paramName, string message)
4    {
5        if (paramValue != null)
6        {
7            throw new ArgumentException(message, paramName);
8        }
9    }
10
11    public static void AssertNull<T>(T paramValue, string paramName, ExceptionMessage message)
12    {
13        if (paramValue != null)
14        {
15            throw new ArgumentException(GetDescription(message), paramName);
16        }
17    }
18
19    public static void AssertNotNull<T>(T paramValue, string paramName, string message)
20    {
21        if (paramValue == null)
22        {
23            throw new ArgumentNullException(paramName, message);
24        }
25    }
26
27    public static void AssertNotNull<T>(T paramValue, string paramName, ExceptionMessage message)
28    {
29        if (paramValue == null)
30        {
31            throw new ArgumentNullException(paramName, GetDescription(message));
32        }
33    }
34
35    public static void AssertNotNullOrEmpty(string paramValue, string paramName, string message)
36    {
37        if (string.IsNullOrEmpty(paramValue))
38        {
39            throw new ArgumentNullException(paramName, message);
40        }
41    }
42
43    public static void AssertNotNullOrEmpty(string paramValue, string paramName, ExceptionMessage message)
44    {
45        if (string.IsNullOrEmpty(paramValue))
46        {
47            throw new ArgumentNullException(paramName, GetDescription(message));
48        }
49    }
50
51    public static void AssertGreaterThanZero(int paramValue, string paramName, string message)
52    {
53        if (paramValue < 1)
54        {
55            throw new ArgumentOutOfRangeException(paramName, message);
56        }
57    }
58
59    public static void AssertGreaterThanZero(int paramValue, string paramName, ExceptionMessage message)
60    {
61        if (paramValue < 1)
62        {
63            throw new ArgumentOutOfRangeException(paramName, GetDescription(message));
64        }
65    }
66
67    private static string GetDescription(ExceptionMessage message)
68    {
69        MemberInfo[] mi = message.GetType().GetMember(message.ToString());
70
71        if (mi != null && mi.Length == 1)
72        {
73            DescriptionAttribute[] attribs = (DescriptionAttribute[])mi[0].GetCustomAttributes(typeof(DescriptionAttribute), true);
74
75            if (attribs.Length == 1)
76            {
77                return attribs[0].Text;
78            }
79            else
80            {
81                return message.ToString();
82            }
83        }
84        else
85        {
86            return message.ToString();
87        }
88    }
89}
Usage
ArgumentHelper.AssertNotNull(user, "user", "Cannot be null.");
ArgumentHelper.AssertNull(GetUser(user.Username), "user.Username", "Already exists.");
Notes
A version of this was originally found at http://www.codeproject.com/csharp/thehelpertrinity.asp. There are obviously more assert methods you can add, but these should get you started.