-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathObjectExtensions.cs
58 lines (53 loc) · 1.66 KB
/
ObjectExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
static class ObjectExtensions
{
public static IEnumerable<T> Add<T>(this IEnumerable<T> collection, T newElement)
{
foreach (var element in collection) {
yield return element;
}
yield return newElement;
}
public static IEnumerable<object> Map<T>(this IEnumerable<object> collection, Func<T, T> func,
Func<T, bool> predicate = null)
{
foreach (var element in collection) {
if (element.GetType().EqualsOrSubtype<T>() &&
(predicate == null ||
(predicate != null && predicate((T)element)))) {
yield return func((T)element);
}
else {
yield return element;
}
}
}
public static IEnumerable<object> Remove(this IEnumerable<object> collection, Type type)
{
bool removed = false;
foreach (var element in collection) {
if (!element.GetType().Equals(type)) {
yield return element;
}
else {
if (!removed) {
removed = true;
}
else {
yield return element;
}
}
}
}
public static IEnumerable<object> Remove<T>(
this IEnumerable<object> collection) => collection.Remove(typeof(T));
public static IEnumerable<object> RemoveAll<T>(this IEnumerable<object> collection)
{
foreach (var element in collection) {
if (!element.GetType().IsSubclassOf(typeof(T))) {
yield return element;
}
}
}
}