Skip to content

Commit 87a88dd

Browse files
author
DwGoing
committed
🦄 refactor: 移除ForEach扩展
1 parent 6286b8b commit 87a88dd

File tree

8 files changed

+30
-103
lines changed

8 files changed

+30
-103
lines changed

src/DwFramework.Core/Extensions/DataTableExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static T[] ToArray<T>(this DataTable dataTable, Dictionary<Type, Func<obj
2222
{
2323
arr[i] = Activator.CreateInstance<T>();
2424
var row = dataTable.Rows[i];
25-
properties.ForEach(property =>
25+
foreach (var property in properties)
2626
{
2727
var srcData = row[property.Name];
2828
// 自定义类型转换
@@ -31,7 +31,7 @@ public static T[] ToArray<T>(this DataTable dataTable, Dictionary<Type, Func<obj
3131
// 自定义字段转换
3232
srcData = propertyFunc != null && propertyFunc.ContainsKey(property.Name) ? propertyFunc[property.Name](srcData) : srcData;
3333
property.SetValue(arr[i], srcData);
34-
});
34+
};
3535
}
3636
return arr;
3737
}

src/DwFramework.Core/Extensions/IEnumerableExtension.cs

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,6 @@ namespace DwFramework.Core
88
{
99
public static class IEnumerableExtension
1010
{
11-
/// <summary>
12-
/// 遍历
13-
/// </summary>
14-
/// <param name="enumerable"></param>
15-
/// <param name="action"></param>
16-
/// <param name="onException"></param>
17-
public static void ForEach(this IEnumerable enumerable, Action<object> action, Action<object, Exception> onException = null)
18-
{
19-
foreach (var item in enumerable)
20-
{
21-
try
22-
{
23-
action?.Invoke(item);
24-
}
25-
catch (Exception ex)
26-
{
27-
if (onException == null) throw;
28-
onException?.Invoke(item, ex);
29-
}
30-
}
31-
}
32-
33-
/// <summary>
34-
/// 遍历
35-
/// </summary>
36-
/// <typeparam name="T"></typeparam>
37-
/// <param name="enumerable"></param>
38-
/// <param name="action"></param>
39-
/// <param name="onException"></param>
40-
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action, Action<T, Exception> onException = null)
41-
{
42-
foreach (var item in enumerable)
43-
{
44-
try
45-
{
46-
action?.Invoke(item);
47-
}
48-
catch (Exception ex)
49-
{
50-
if (onException == null) throw;
51-
onException?.Invoke(item, ex);
52-
}
53-
}
54-
}
55-
56-
/// <summary>
57-
/// 遍历(并行)
58-
/// </summary>
59-
/// <typeparam name="T"></typeparam>
60-
/// <param name="enumerable"></param>
61-
/// <param name="action"></param>
62-
/// <param name="onException"></param>
63-
public static void ForEachParallel<T>(this IEnumerable<T> enumerable, Action<T> action, Action<T, Exception> onException = null)
64-
{
65-
Parallel.ForEach(enumerable, (item, state) =>
66-
{
67-
try
68-
{
69-
if (state.ShouldExitCurrentIteration) return;
70-
action?.Invoke(item);
71-
}
72-
catch (Exception ex)
73-
{
74-
if (onException == null)
75-
{
76-
state.Stop();
77-
throw;
78-
}
79-
onException?.Invoke(item, ex);
80-
}
81-
});
82-
}
83-
8411
/// <summary>
8512
/// 按字段去重
8613
/// </summary>
@@ -103,7 +30,7 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
10330
/// <param name="values"></param>
10431
public static void AddRange<T>(this ICollection<T> collection, params T[] values)
10532
{
106-
values.ForEach(item => collection.Add(item));
33+
foreach (var item in values) collection.Add(item);
10734
}
10835

10936
/// <summary>
@@ -115,7 +42,7 @@ public static void AddRange<T>(this ICollection<T> collection, params T[] values
11542
/// <param name="values"></param>
11643
public static void AddRangeIf<T>(this ICollection<T> collection, Func<T, bool> predicate, params T[] values)
11744
{
118-
values.ForEach(item => { if (predicate(item)) collection.Add(item); });
45+
foreach (var item in values) if (predicate(item)) collection.Add(item);
11946
}
12047

12148
/// <summary>
@@ -126,7 +53,7 @@ public static void AddRangeIf<T>(this ICollection<T> collection, Func<T, bool> p
12653
/// <param name="values"></param>
12754
public static void AddRangeIfNotContains<T>(this ICollection<T> collection, params T[] values)
12855
{
129-
values.ForEach(item => { if (!collection.Contains(item)) collection.Add(item); });
56+
foreach (var item in values) if (!collection.Contains(item)) collection.Add(item);
13057
}
13158

13259
/// <summary>
@@ -137,7 +64,8 @@ public static void AddRangeIfNotContains<T>(this ICollection<T> collection, para
13764
/// <param name="predicate"></param>
13865
public static void RemoveWhere<T>(this ICollection<T> collection, Func<T, bool> predicate)
13966
{
140-
collection.Where(predicate).ForEach(item => collection.Remove(item));
67+
var removable = collection.Where(predicate);
68+
foreach (var item in removable) collection.Remove(item);
14169
}
14270

14371
/// <summary>
@@ -150,11 +78,11 @@ public static void RemoveWhere<T>(this ICollection<T> collection, Func<T, bool>
15078
public static void InsertAfter<T>(this IList<T> list, Func<T, bool> predicate, T value)
15179
{
15280
var tmp = list.Select((item, index) => new { item, index }).Where(p => predicate(p.item)).OrderByDescending(p => p.index);
153-
tmp.ForEach(item =>
81+
foreach (var item in tmp)
15482
{
15583
if (item.index + 1 == list.Count) list.Add(value);
15684
else list.Insert(item.index + 1, value);
157-
});
85+
};
15886
}
15987

16088
/// <summary>
@@ -167,11 +95,11 @@ public static void InsertAfter<T>(this IList<T> list, Func<T, bool> predicate, T
16795
public static void InsertAfter<T>(this IList<T> list, int index, T value)
16896
{
16997
var tmp = list.Select((v, i) => new { Value = v, Index = i }).Where(p => p.Index == index).OrderByDescending(p => p.Index);
170-
tmp.ForEach(item =>
98+
foreach (var item in tmp)
17199
{
172100
if (item.Index + 1 == list.Count) list.Add(value);
173101
else list.Insert(item.Index + 1, value);
174-
});
102+
};
175103
}
176104

177105
/// <summary>

src/DwFramework.Core/Extensions/StringExtension.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static byte[] FromBase32String(this string str)
8787
var bitsRemaining = (byte)8;
8888
var arrayIndex = 0;
8989

90-
str.ForEach(item =>
90+
foreach (var item in str)
9191
{
9292
var cValue = ToBase32Value(item);
9393
int mask;
@@ -105,7 +105,7 @@ public static byte[] FromBase32String(this string str)
105105
curByte = (byte)(cValue << (3 + bitsRemaining));
106106
bitsRemaining += 3;
107107
}
108-
});
108+
};
109109

110110
if (arrayIndex != byteCount)
111111
{
@@ -130,7 +130,7 @@ public static string ToBase32String(this byte[] bytes)
130130
var bitsRemaining = (byte)5;
131131
var arrayIndex = 0;
132132

133-
bytes.ForEach(item =>
133+
foreach (var item in bytes)
134134
{
135135
nextChar = (byte)(nextChar | (item >> (8 - bitsRemaining)));
136136
returnArray[arrayIndex++] = ToBase32Char(nextChar);
@@ -144,7 +144,7 @@ public static string ToBase32String(this byte[] bytes)
144144

145145
bitsRemaining -= 3;
146146
nextChar = (byte)((item << bitsRemaining) & 31);
147-
});
147+
};
148148

149149
if (arrayIndex != charCount)
150150
{
@@ -335,11 +335,11 @@ public static bool IsSBC(this char @char)
335335
public static int GetLength(this string str)
336336
{
337337
var len = 0;
338-
str.ForEach(item =>
338+
foreach (var item in str)
339339
{
340340
if (item.IsChinese() || item.IsSBC()) len += 2;
341341
else len++;
342-
});
342+
};
343343
return len;
344344
}
345345
}

src/DwFramework.Core/Plugins/Cache/MemoryCache/MemoryCacheStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ private void CleanExpireData(object sender, EventArgs args)
3838
var currentTime = DateTime.UtcNow;
3939
_isCleaning = true;
4040
var keys = _datas.Keys;
41-
keys.ForEach(item =>
41+
foreach (var item in keys)
4242
{
4343
var data = (MemoryCacheData)_datas[item];
4444
if (data.IsExpired) Del((string)item);
45-
});
45+
};
4646
_isCleaning = false;
4747
}
4848

@@ -186,7 +186,7 @@ public Dictionary<string, object> HGetAll(string key)
186186
var table = Get<Hashtable>(key);
187187
if (table == null) return default;
188188
var dic = new Dictionary<string, object>();
189-
table.ForEach(item => dic.Add((string)((DictionaryEntry)item).Key, ((DictionaryEntry)item).Value));
189+
foreach (var item in table) dic.Add((string)((DictionaryEntry)item).Key, ((DictionaryEntry)item).Value);
190190
return dic;
191191
}
192192

src/DwFramework.Core/Plugins/Time/StopwatchManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void SetStartTime(string tag, DateTime startTime)
6868
public static void Remove(string tag)
6969
{
7070
if (!_stopwatches.ContainsKey(tag)) return;
71-
_stopwatches.RemoveWhere(item => item.Key == tag);
71+
_stopwatches.TryRemove(tag, out _);
7272
}
7373

7474
/// <summary>

src/DwFramework.Quartz/Models/QuartzService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public async Task ExcludeInWeekAsync(string schedulerName, string calName, DayOf
209209
{
210210
var scheduler = await GetSchedulerAsync(schedulerName);
211211
var calender = new WeeklyCalendar();
212-
excludeDays.ForEach(item => calender.SetDayExcluded(item, true));
212+
foreach (var item in excludeDays) calender.SetDayExcluded(item, true);
213213
await scheduler.AddCalendar(calName, calender, true, true);
214214
}
215215

@@ -224,7 +224,7 @@ public async Task ExcludeInMonthAsync(string schedulerName, string calName, int[
224224
{
225225
var scheduler = await GetSchedulerAsync(schedulerName);
226226
var calender = new MonthlyCalendar();
227-
excludeDays.ForEach(item => calender.SetDayExcluded(item, true));
227+
foreach (var item in excludeDays) calender.SetDayExcluded(item, true);
228228
await scheduler.AddCalendar(calName, calender, true, true);
229229
}
230230

@@ -239,7 +239,7 @@ public async Task ExcludeInYearAsync(string schedulerName, string calName, (int,
239239
{
240240
var scheduler = await GetSchedulerAsync(schedulerName);
241241
var calender = new AnnualCalendar();
242-
excludeMonthDays.ForEach(item => calender.SetDayExcluded(DateBuilder.DateOf(0, 0, 0, item.Item2, item.Item1).DateTime, true));
242+
foreach (var item in excludeMonthDays) calender.SetDayExcluded(DateBuilder.DateOf(0, 0, 0, item.Item2, item.Item1).DateTime, true);
243243
await scheduler.AddCalendar(calName, calender, true, true);
244244
}
245245

src/DwFramework.RabbitMQ/Models/RabbitMQService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void Subscribe(string queue, bool autoAck, Action<IModel, BasicDeliverEve
239239
public void Unsubscribe(string queue)
240240
{
241241
if (!_subscribers.ContainsKey(queue)) return;
242-
_subscribers[queue].ForEach(item => item.Model.Abort());
242+
foreach (var item in _subscribers[queue]) item.Model.Abort();
243243
_subscribers.Remove(queue);
244244
}
245245
}

src/DwFramework.Web/Models/WebSocketClient.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Net.WebSockets;
55
using System.Linq;
66
using System.Collections.Generic;
7-
using DwFramework.Core;
87

98
namespace DwFramework.Web
109
{
@@ -112,27 +111,27 @@ private void ClearAllEvent()
112111
if (OnConnect != null)
113112
{
114113
var actions = OnConnect.GetInvocationList();
115-
actions.ForEach(item => OnConnect -= (Action<OnConnectEventArgs>)item);
114+
foreach (var item in actions) OnConnect -= (Action<OnConnectEventArgs>)item;
116115
}
117116
if (OnClose != null)
118117
{
119118
var actions = OnClose.GetInvocationList();
120-
actions.ForEach(item => OnClose -= (Action<OnCloceEventArgs>)item);
119+
foreach (var item in actions) OnClose -= (Action<OnCloceEventArgs>)item;
121120
}
122121
if (OnError != null)
123122
{
124123
var actions = OnError.GetInvocationList();
125-
actions.ForEach(item => OnError -= (Action<OnErrorEventArgs>)item);
124+
foreach (var item in actions) OnError -= (Action<OnErrorEventArgs>)item;
126125
}
127126
if (OnSend != null)
128127
{
129128
var actions = OnSend.GetInvocationList();
130-
actions.ForEach(item => OnSend -= (Action<OnSendEventArgs>)item);
129+
foreach (var item in actions) OnSend -= (Action<OnSendEventArgs>)item;
131130
}
132131
if (OnReceive != null)
133132
{
134133
var actions = OnReceive.GetInvocationList();
135-
actions.ForEach(item => OnReceive -= (Action<OnReceiveEventArgs>)item);
134+
foreach (var item in actions) OnReceive -= (Action<OnReceiveEventArgs>)item;
136135
}
137136
}
138137
}

0 commit comments

Comments
 (0)