Created
August 10, 2016 08:17
-
-
Save mollyporph/b8412745280e6c5e0d87b296898483f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static T Tap<T>(this T self, Action<T> f) | |
{ | |
f(self); | |
return self; | |
} | |
public static T2 Pipe<T, T2>(this T self, Func<T, T2> f) | |
=> f(self); | |
public static bool IsNull(this object item) => item == null; | |
public static int ToEpoch(this DateTime date) | |
{ | |
var epoch = new DateTime(1970, 1, 1); | |
var epochTimeSpan = date - epoch; | |
return (int)epochTimeSpan.TotalSeconds; | |
} | |
public static void ForEach<T>(this IEnumerable<T> self, Action<T> f) | |
{ | |
foreach (var item in self) | |
{ | |
f(item); | |
} | |
} | |
public static async Task<byte[]> ToByteArrayAsync(this Stream stream) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
await stream.CopyToAsync(ms); | |
return ms.ToArray(); | |
} | |
} | |
public static string GetFileName(this IFormFile self) | |
=> ContentDispositionHeaderValue.Parse(self.ContentDisposition).FileName.Trim('"'); | |
public static Stream SerializeToJsonStream(this object value) | |
{ | |
var s = new MemoryStream(); | |
var writer = new StreamWriter(s); | |
var jsonWriter = new JsonTextWriter(writer); | |
var ser = new JsonSerializer() | |
{ | |
TypeNameHandling = TypeNameHandling.Auto | |
}; | |
ser.Serialize(jsonWriter, value); | |
jsonWriter.Flush(); | |
s.Seek(0, SeekOrigin.Begin); | |
return s; | |
} | |
public static T DeserializeJsonFromStream<T>(this Stream self) | |
=> new JsonSerializer().Deserialize<T>(new JsonTextReader(new StreamReader(self))); | |
public static T Get<T>(this IConfiguration config) | |
where T : new() => new T().Tap(x => config.Bind(x)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment