Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / FakeHttpContextFactory.cs
Created October 24, 2014 06:55
Create a fake HttpContext, without using mocking-frameworks
public static HttpContext GetFakeHttpContext(
string url,
HttpRequest request = null,
HttpResponse response = null)
{
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
throw new ArgumentOutOfRangeException("url", "The URL must be a well-formed absolute URI.");
}
@sebnilsson
sebnilsson / HashAlgorithmExtensions.cs
Created December 30, 2017 20:54
Compute hash in .NET using async await
public static class HashAlgorithmExtensions
{
private const int BufferSize = 4096;
public static async Task<byte[]> ComputeHashAsync(this HashAlgorithm hash, Stream inputStream)
{
if (hash == null) throw new ArgumentNullException(nameof(hash));
if (inputStream == null) throw new ArgumentNullException(nameof(inputStream));
hash.Initialize();
@sebnilsson
sebnilsson / ConvertWordsToPdfs.cls
Last active August 22, 2020 03:48
VBA: Loop through all files in a directory and convert them to PDF-files
Sub ConvertWordsToPdfs()
Dim directory As String
directory = "C:\Wordup" ' The starting directory
Dim fso, folder, files
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(directory)
Set files = folder.files
@sebnilsson
sebnilsson / DateTimeExtensions.cs
Created April 3, 2013 07:55
Format C# UTC DateTime to Local Format in JavaScript with Moment.js
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static double GetEpochTicks(this DateTime dateTime)
{
return dateTime.Subtract(Epoch).TotalMilliseconds;
}
@sebnilsson
sebnilsson / keybase.md
Created May 13, 2019 14:42
keybase.md

Keybase proof

I hereby claim:

  • I am sebnilsson on github.
  • I am sebnilsson (https://keybase.io/sebnilsson) on keybase.
  • I have a public key ASCWYybMp3SXcq0oy9xFjsdWBwizgfPCOEE1sCL7ke-Gmwo

To claim this, I am signing this object:

@sebnilsson
sebnilsson / Autofac.AutoMapper.Program.cs
Last active February 5, 2019 13:55
Registering Autofac & AutoMapper Circularly
public static void Main(string[] args)
{
var components = RegisterComponents();
Run(components);
}
private static IContainer RegisterComponents()
{
var builder = new ContainerBuilder();
@sebnilsson
sebnilsson / HtmlEncodeTagHelper.cs
Last active January 18, 2019 15:26
Render everything inside tag as encoded HTML
[HtmlTargetElement("html-encode", TagStructure = TagStructure.NormalOrSelfClosing)]
public class HtmlEncodeTagHelper : TagHelper
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = output.Content.IsModified
? output.Content.GetContent()
: (await output.GetChildContentAsync()).GetContent();
string encodedChildContent = WebUtility.HtmlEncode(childContent ?? string.Empty);
@sebnilsson
sebnilsson / ObjectExpandoObjectExtensions.cs
Last active November 16, 2017 11:52
Convert anonymous type to dynamic
public static ExpandoObject ToExpandoObject(this object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj.GetType()))
expando.Add(property.Name, property.GetValue(obj));
@sebnilsson
sebnilsson / UriExtensions.cs
Last active November 16, 2017 08:43
Get relative part of an Uri
public static string ToRelative(this Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}
return uri.IsAbsoluteUri ? uri.PathAndQuery : uri.OriginalString;
}
public static class DateTimeRfc3339Extensions
{
public static string ToRfc3339String(this DateTime dateTime)
{
string rfc3339 = dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo);
return rfc3339;
}
}