annotate Implab/Safe.cs @ 51:2c332a9c64c0

Added methods for parameter checks
author cin
date Sat, 26 Apr 2014 23:36:00 +0400
parents dafaadca5b9f
children c0bf853aa04f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
1 using System;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
2 using System.Collections.Generic;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
3 using System.Linq;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
4 using System.Text;
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
5 using System.Text.RegularExpressions;
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
6
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
7 namespace Implab
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
8 {
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
9 public static class Safe
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
10 {
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
11 public static void ArgumentMatch(string param, string name, Regex rx) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
12 if (rx == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
13 throw new ArgumentNullException("rx");
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
14 if (!rx.IsMatch(param))
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
15 throw new ArgumentException(String.Format("A prameter value must match {0}", rx), name);
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
16 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
17
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
18 public static void ArgumentNotEmpty(string param, string name) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
19 if (String.IsNullOrEmpty(param))
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
20 throw new ArgumentException("A parameter can't be empty", name);
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
21 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
22
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
23 public static void ArgumentNotNull(object param, string name) {
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
24 if (param == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
25 throw new ArgumentNullException(name);
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
26 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
27
31
dafaadca5b9f minor cleanup
cin
parents: 2
diff changeset
28 public static void Dispose<T>(T obj) where T : class
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
29 {
2
aa367305156b small fixes
cin
parents: 1
diff changeset
30 var disp = obj as IDisposable;
aa367305156b small fixes
cin
parents: 1
diff changeset
31 if (disp != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
32 disp.Dispose();
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
33 }
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
34 }
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
35 }