Skip to content

Instantly share code, notes, and snippets.

@togakangaroo
Created November 21, 2014 15:03
Show Gist options
  • Save togakangaroo/0a1ec83cc1f5a8766929 to your computer and use it in GitHub Desktop.
Save togakangaroo/0a1ec83cc1f5a8766929 to your computer and use it in GitHub Desktop.
some extension methods
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace System
{
public static class ObjectExtensions
{
public static bool NullSafeEquals(this object x1, object x2) {
return x1.IfNotNull(x => x.Equals(x2), (Object.ReferenceEquals(x1, null) && Object.ReferenceEquals(x2, null)));
}
public static void IfNotNullThen<T>(this T o, Action<T> action) where T : class
{
if (o != null)
action.Invoke(o);
}
public static R IfNotNull<T, R>(this T o, Func<T, R> returnFunc)
{
return IfNotNull(o, returnFunc, default(R));
}
public static R IfNotNull<T, R>(this T o, Func<T, R> returnFunc, R otherwise)
{
return ReferenceEquals(null, o) ? otherwise : returnFunc(o);
}
public static T NotNullOrThrow<T, E>(this T o, E ex) where E : Exception {
if (Object.ReferenceEquals(null, o))
throw ex;
return o;
}
public static bool IsNull(this object obj)
{
return ReferenceEquals(obj, null);
}
public static bool IsNotNull(this object obj)
{
return !ReferenceEquals(obj, null);
}
public static VALUE TryGetValue<KEY, VALUE>(this IDictionary<KEY, VALUE> dict, KEY key)
{
VALUE value;
dict.TryGetValue(key, out value);
return value;
}
public static VALUE GetOrCache<KEY, VALUE>(this IDictionary<KEY, VALUE> dict, KEY key, Func<VALUE> getVal) {
return dict.ContainsKey(key) ? dict[key] : (dict[key] = getVal());
}
public static IDictionary<KEY, VALUE> Slice<KEY, VALUE>(this IDictionary<KEY, VALUE> dict, params KEY[] keys) {
return dict.Where(kv => keys.Contains(kv.Key)).ToDictionary(kv => kv.Key, kv => kv.Value);
}
public static T[] WrapInArray<T>(this T item) {
return new[] {item};
}
public static IEnumerable<T1> Prepend<T1, T2>(this IEnumerable<T1> collection, T2 obj, params T2[] more) where T2 : T1 {
yield return obj;
foreach (var x in more) yield return x;
foreach (var x in collection) yield return x;
}
public static IEnumerable<T1> Append<T1, T2>(this IEnumerable<T1> collection, T2 obj, params T2[] more) where T2 : T1 {
foreach (var x in collection) yield return x;
yield return obj;
foreach (var x in more) yield return x;
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> act) {
foreach (var x in collection)
act(x);
return collection;
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T, int> act) {
var i = 0;
foreach (var x in collection)
act(x, i++);
return collection;
}
public static IEnumerable<T> Except<T>(this IEnumerable<T> collection, params T[] withoutThese) {
return collection.Except(withoutThese.AsEnumerable());
}
public static IDictionary<K, V> Clone<K, V>(this IDictionary<K, V> original) {
if (original.IsNull()) return null;
return new Dictionary<K, V>(original);
}
public static IDictionary<K, V> Merge<K, V>(this IDictionary<K, V> original, IDictionary<K,V> other ) {
var d = original.Clone();
other.ForEach(kv => d[kv.Key] = kv.Value);
return d;
}
public static IEnumerable<string> GetAllMessages(this Exception ex) {
if (ex == null) return Enumerable.Empty<string>();
return GetAllMessages(ex.InnerException).Append(ex.Message);
}
public static IEnumerable<string> CapturedGroupValues(this Regex rx, string input) {
return rx.Match(input).Groups.Cast<Group>().Select(g => g.Value);
}
}
public static class AwaitEx
{
public static TaskAwaiter GetAwaiter(this TimeSpan timeSpan)
{
return Task.Delay(timeSpan).GetAwaiter();
}
public static TaskAwaiter<T[]> GetAwaiter<T>(this IEnumerable<Task<T>> tasks)
{
return Task.WhenAll(tasks).GetAwaiter();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment