-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonTypeExtension.cs
More file actions
94 lines (76 loc) · 2.7 KB
/
CommonTypeExtension.cs
File metadata and controls
94 lines (76 loc) · 2.7 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ekra14.AltFXtension
{
public static class CommonTypeExtension
{
public static bool Among<T>(this T item, params T[] list)
{
return list.Contains(item);
}
public static bool In<T>(this T item, params T[] list)
{
return list.Contains(item);
}
public static bool In<T>(this T item, IEnumerable<T> list)
{
if (list == null) throw new ArgumentNullException("list");
return list.Contains(item);
}
public static bool In<T>(this T item, ICollection<T> list)
{
if (list == null) throw new ArgumentNullException("list");
return list.Contains(item);
}
public static bool NotIn<T>(this T item, params T[] list)
{
return !list.Contains(item);
}
public static bool NotIn<T>(this T item, IEnumerable<T> list)
{
if (list == null) throw new ArgumentNullException("list");
return !list.Contains(item);
}
public static bool NotIn<T>(this T item, ICollection<T> list)
{
if (list == null) throw new ArgumentNullException("list");
return !list.Contains(item);
}
public static TResult Maybe<TItem, TResult>(this TItem item, Func<TItem, TResult> func, TResult defaultValue = default(TResult))
where TItem : class
{
if (func == null) throw new ArgumentNullException("func");
return item != null ? func(item) : defaultValue;
}
public static TResult IfNotNull<TItem, TResult>(this TItem item, Func<TItem, TResult> func, TResult defaultValue = default(TResult))
where TItem : class
{
return item.Maybe(func);
}
public static TItem IfNotNull<TItem>(this TItem item, Action<TItem> action)
where TItem : class
{
if (action == null) throw new ArgumentNullException("action");
if (item != null) action(item);
return item;
}
public static TItem As<TItem>(this object item)
where TItem : class
{
return item as TItem;
}
public static object IfIs<TItem>(this object item, Action<TItem> action)
where TItem : class
{
(item as TItem).IfNotNull(action);
return item;
}
public static TItem Doing<TItem>(this TItem item, Action<TItem> action)
{
if (action == null) throw new ArgumentNullException("action");
action(item);
return item;
}
}
}