forked from DarthFubuMVC/fubucore
/
BasicExtensions.cs
48 lines (43 loc) · 1.27 KB
/
BasicExtensions.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
using System;
namespace FubuCore
{
public static class BasicExtensions
{
public static void SafeDispose(this IDisposable disposable)
{
try
{
disposable.Dispose();
}
catch (Exception)
{
// That's right, swallow that exception
}
}
public static TOut IfNotNull<TTarget, TOut>(this TTarget target, Func<TTarget, TOut> valueFunc)
where TTarget : class
{
return target == null ? default(TOut) : valueFunc(target);
}
public static void CallIfNotNull<TTarget>(this TTarget target, Action<TTarget> actionToPerform)
where TTarget : class
{
if(target != null)
{
actionToPerform(target);
}
}
public static T IfNotNull<T>(this object target, Func<T> valueFunc)
where T : class
{
return target == null ? null : valueFunc();
}
public static void IfNotNull<T>(this T? target, Action<T> action) where T : struct
{
if (target.HasValue)
{
action(target.Value);
}
}
}
}