markus / KCOM / Extensions / CollectionExtenstions.cs @ 1c7f408a
이력 | 보기 | 이력해설 | 다운로드 (9.24 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Linq.Expressions; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
|
8 |
namespace KCOM |
9 |
{ |
10 |
public static class CollectionExtensions |
11 |
{ |
12 |
//public static void Update<T>(this IList<T> source,IList<T> Target,IList<string> Keys) |
13 |
//{ |
14 |
// for (int i = source.Count; i == 0 ; --i) |
15 |
// { |
16 |
// var item = source[i]; |
17 |
// Target.Where(f=>) |
18 |
// item.GetType().GetProperty() |
19 |
// } |
20 |
//} |
21 |
|
22 |
public static T ChangeValues<T>(T item, T newitem) |
23 |
{ |
24 |
var changeProp = item.GetType().GetProperties(); |
25 |
var newProp = newitem.GetType().GetProperties(); |
26 |
|
27 |
foreach (var Prop in changeProp) |
28 |
{ |
29 |
|
30 |
var newinfo = newProp.Where(x => x.Name == Prop.Name); |
31 |
|
32 |
if (newinfo.Count() > 0) |
33 |
{ |
34 |
Prop.SetValue(item, newinfo.First().GetValue(newitem)); |
35 |
} |
36 |
} |
37 |
|
38 |
return item; |
39 |
} |
40 |
|
41 |
/// <summary> |
42 |
/// Changes all elements of IEnumerable by the change function |
43 |
/// </summary> |
44 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
45 |
/// <param name="change">The way you want to change the stuff</param> |
46 |
/// <returns>An IEnumerable with all changes applied</returns> |
47 |
public static IEnumerable<T> Change<T>(this IEnumerable<T> enumerable, Func<T, T> change) |
48 |
{ |
49 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
50 |
ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
51 |
|
52 |
foreach (var item in enumerable) |
53 |
{ |
54 |
yield return change(item); |
55 |
} |
56 |
} |
57 |
|
58 |
/// <summary> |
59 |
/// Changes all elements of IEnumerable by the change function, that fullfill the where function |
60 |
/// </summary> |
61 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
62 |
/// <param name="change">The way you want to change the stuff</param> |
63 |
/// <param name="where">The function to check where changes should be made</param> |
64 |
/// <returns> |
65 |
/// An IEnumerable with all changes applied |
66 |
/// </returns> |
67 |
public static IEnumerable<T> ChangeWhere<T>(this IEnumerable<T> enumerable, |
68 |
Func<T, T> change, |
69 |
Func<T, bool> @where) |
70 |
{ |
71 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
72 |
ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
73 |
ArgumentCheck.IsNullorWhiteSpace(@where, "where"); |
74 |
|
75 |
foreach (var item in enumerable) |
76 |
{ |
77 |
if (@where(item)) |
78 |
{ |
79 |
yield return change(item); |
80 |
} |
81 |
else |
82 |
{ |
83 |
yield return item; |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
public static class ArgumentCheck |
89 |
{ |
90 |
|
91 |
|
92 |
/// <summary> |
93 |
/// Checks if a value is string or any other object if it is string |
94 |
/// it checks for nullorwhitespace otherwhise it checks for null only |
95 |
/// </summary> |
96 |
/// <typeparam name="T">Type of the item you want to check</typeparam> |
97 |
/// <param name="item">The item you want to check</param> |
98 |
/// <param name="nameOfTheArgument">Name of the argument</param> |
99 |
public static void IsNullorWhiteSpace<T>(T item, string nameOfTheArgument = "") |
100 |
{ |
101 |
|
102 |
Type type = typeof(T); |
103 |
if (type == typeof(string) || |
104 |
type == typeof(String)) |
105 |
{ |
106 |
if (string.IsNullOrWhiteSpace(item as string)) |
107 |
{ |
108 |
throw new ArgumentException(nameOfTheArgument + " is null or Whitespace"); |
109 |
} |
110 |
} |
111 |
else |
112 |
{ |
113 |
if (item == null) |
114 |
{ |
115 |
throw new ArgumentException(nameOfTheArgument + " is null"); |
116 |
} |
117 |
} |
118 |
|
119 |
} |
120 |
} |
121 |
|
122 |
/// <summary> |
123 |
/// Changes all elements of IEnumerable by the change function that do not fullfill the except function |
124 |
/// </summary> |
125 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
126 |
/// <param name="change">The way you want to change the stuff</param> |
127 |
/// <param name="where">The function to check where changes should not be made</param> |
128 |
/// <returns> |
129 |
/// An IEnumerable with all changes applied |
130 |
/// </returns> |
131 |
public static IEnumerable<T> ChangeExcept<T>(this IEnumerable<T> enumerable, |
132 |
Func<T, T> change, |
133 |
Func<T, bool> @where) |
134 |
{ |
135 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
136 |
ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
137 |
ArgumentCheck.IsNullorWhiteSpace(@where, "where"); |
138 |
|
139 |
foreach (var item in enumerable) |
140 |
{ |
141 |
if (!@where(item)) |
142 |
{ |
143 |
yield return change(item); |
144 |
} |
145 |
else |
146 |
{ |
147 |
yield return item; |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
/// <summary> |
153 |
/// Update all elements of IEnumerable by the update function (only works with reference types) |
154 |
/// </summary> |
155 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
156 |
/// <param name="update">The way you want to change the stuff</param> |
157 |
/// <returns> |
158 |
/// The same enumerable you passed in |
159 |
/// </returns> |
160 |
public static IEnumerable<T> Update<T>(this IEnumerable<T> enumerable, |
161 |
Action<T> update) where T : class |
162 |
{ |
163 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
164 |
ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
165 |
foreach (var item in enumerable) |
166 |
{ |
167 |
update(item); |
168 |
} |
169 |
return enumerable; |
170 |
} |
171 |
|
172 |
/// <summary> |
173 |
/// Update all elements of IEnumerable by the update function (only works with reference types) |
174 |
/// </summary> |
175 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
176 |
/// <param name="Find">T와 동일한 첫번째 Item을 찾는다. 없으면 null을 반환</param> |
177 |
/// <returns> |
178 |
/// The same enumerable you passed in |
179 |
/// </returns> |
180 |
public static T Find<T>(this IEnumerable<T> enumerable, Func<T, bool> where) where T : class |
181 |
{ |
182 |
T result = null; |
183 |
|
184 |
foreach (var item in enumerable) |
185 |
{ |
186 |
if (where(item)) |
187 |
{ |
188 |
result = item; |
189 |
} |
190 |
} |
191 |
|
192 |
return result; |
193 |
} |
194 |
|
195 |
/// <summary> |
196 |
/// Update all elements of IEnumerable by the update function (only works with reference types) |
197 |
/// where the where function returns true |
198 |
/// </summary> |
199 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
200 |
/// <param name="update">The way you want to change the stuff</param> |
201 |
/// <param name="where">The function to check where updates should be made</param> |
202 |
/// <returns> |
203 |
/// The same enumerable you passed in |
204 |
/// </returns> |
205 |
public static IEnumerable<T> UpdateWhere<T>(this IEnumerable<T> enumerable, |
206 |
Action<T> update, Func<T, bool> where) where T : class |
207 |
{ |
208 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
209 |
ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
210 |
foreach (var item in enumerable) |
211 |
{ |
212 |
if (where(item)) |
213 |
{ |
214 |
update(item); |
215 |
} |
216 |
} |
217 |
return enumerable; |
218 |
} |
219 |
|
220 |
/// <summary> |
221 |
/// Update all elements of IEnumerable by the update function (only works with reference types) |
222 |
/// Except the elements from the where function |
223 |
/// </summary> |
224 |
/// <param name="enumerable">The enumerable where you want to change stuff</param> |
225 |
/// <param name="update">The way you want to change the stuff</param> |
226 |
/// <param name="where">The function to check where changes should not be made</param> |
227 |
/// <returns> |
228 |
/// The same enumerable you passed in |
229 |
/// </returns> |
230 |
public static IEnumerable<T> UpdateExcept<T>(this IEnumerable<T> enumerable, |
231 |
Action<T> update, Func<T, bool> where) where T : class |
232 |
{ |
233 |
ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
234 |
ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
235 |
|
236 |
foreach (var item in enumerable) |
237 |
{ |
238 |
if (!where(item)) |
239 |
{ |
240 |
update(item); |
241 |
} |
242 |
} |
243 |
return enumerable; |
244 |
} |
245 |
} |
246 |
} |