##// END OF EJS Templates
fixed recursion in Safe.Dispose with IEnumerable
cin -
r221:8808383fcb94 v2
parent child
Show More
@@ -1,152 +1,152
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Linq;
4 4 using System.Text;
5 5 using System.Text.RegularExpressions;
6 6 using System.Diagnostics;
7 7 using System.Collections;
8 8
9 9 #if NET_4_5
10 10 using System.Threading.Tasks;
11 11 #endif
12 12
13 13 namespace Implab
14 14 {
15 15 public static class Safe
16 16 {
17 17 public static void ArgumentAssert(bool condition, string paramName) {
18 18 if (!condition)
19 19 throw new ArgumentException("The parameter is invalid", paramName);
20 20 }
21 21
22 22 public static void ArgumentMatch(string value, string paramName, Regex rx) {
23 23 if (rx == null)
24 24 throw new ArgumentNullException("rx");
25 25 if (!rx.IsMatch(value))
26 26 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
27 27 }
28 28
29 29 public static void ArgumentNotEmpty(string value, string paramName) {
30 30 if (String.IsNullOrEmpty(value))
31 31 throw new ArgumentException("The parameter can't be empty", paramName);
32 32 }
33 33
34 34 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
35 35 if (value == null || value.Length == 0)
36 36 throw new ArgumentException("The array must be not emty", paramName);
37 37 }
38 38
39 39 public static void ArgumentNotNull(object value, string paramName) {
40 40 if (value == null)
41 41 throw new ArgumentNullException(paramName);
42 42 }
43 43
44 44 public static void ArgumentInRange(int value, int min, int max, string paramName) {
45 45 if (value < min || value > max)
46 46 throw new ArgumentOutOfRangeException(paramName);
47 47 }
48 48
49 49 public static void ArgumentOfType(object value, Type type, string paramName) {
50 50 if (!type.IsInstanceOfType(value))
51 51 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName);
52 52 }
53 53
54 54 public static void Dispose(params IDisposable[] objects) {
55 55 foreach (var d in objects)
56 56 if (d != null)
57 57 d.Dispose();
58 58 }
59 59
60 60 public static void Dispose(params object[] objects) {
61 61 foreach (var obj in objects) {
62 62 var d = obj as IDisposable;
63 63 if (d != null)
64 64 d.Dispose();
65 65 }
66 66 }
67 67
68 68 public static void DisposeCollection(IEnumerable<IDisposable> objects) {
69 69 foreach (var d in objects)
70 70 Dispose(d);
71 71 }
72 72
73 73 public static void DisposeCollection(IEnumerable objects) {
74 74 foreach (var d in objects)
75 75 Dispose(d);
76 76 }
77 77
78 78 public static void Dispose(object obj) {
79 79 if (obj is IDisposable) {
80 80 Dispose((IDisposable)obj);
81 81 } else if (obj is IEnumerable) {
82 Dispose((IEnumerable)obj);
82 DisposeCollection((IEnumerable)obj);
83 83 }
84 84 }
85 85
86 86 [DebuggerStepThrough]
87 87 public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) {
88 88 if (handler != null)
89 89 handler(sender, args);
90 90 }
91 91
92 92 [DebuggerStepThrough]
93 93 public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) {
94 94 if (handler != null)
95 95 handler(sender, args);
96 96 }
97 97
98 98 [DebuggerStepThrough]
99 99 public static IPromise<T> Run<T>(Func<T> action) {
100 100 ArgumentNotNull(action, "action");
101 101
102 102 try {
103 103 return Promise<T>.FromResult(action());
104 104 } catch (Exception err) {
105 105 return Promise<T>.FromException(err);
106 106 }
107 107 }
108 108
109 109 [DebuggerStepThrough]
110 110 public static IPromise Run(Action action) {
111 111 ArgumentNotNull(action, "action");
112 112
113 113 try {
114 114 action();
115 115 return Promise.Success;
116 116 } catch (Exception err) {
117 117 return new FailedPromise(err);
118 118 }
119 119 }
120 120
121 121 [DebuggerStepThrough]
122 122 public static IPromise Run(Func<IPromise> action) {
123 123 ArgumentNotNull(action, "action");
124 124
125 125 try {
126 126 return action() ?? new FailedPromise(new Exception("The action returned null"));
127 127 } catch (Exception err) {
128 128 return new FailedPromise(err);
129 129 }
130 130 }
131 131
132 132 public static void NoWait(IPromise promise) {
133 133 }
134 134
135 135 [DebuggerStepThrough]
136 136 public static IPromise<T> Run<T>(Func<IPromise<T>> action) {
137 137 ArgumentNotNull(action, "action");
138 138
139 139 try {
140 140 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
141 141 } catch (Exception err) {
142 142 return Promise<T>.FromException(err);
143 143 }
144 144 }
145 145
146 146 #if NET_4_5
147 147 public static void NoWait(Task t) {
148 148 }
149 149 #endif
150 150
151 151 }
152 152 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now