Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / GetX509Certificate2.cs
Created April 11, 2013 13:17
Get X509Certificate2 from Store
private static X509Certificate2 GetX509Certificate2(string thumbprint)
{
var store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var certificates = store.Certificates.Cast<X509Certificate2>().ToList();
var cert =
certificates.FirstOrDefault(
x => x.Thumbprint.Equals(thumbprint, StringComparison.InvariantCultureIgnoreCase));
@sebnilsson
sebnilsson / GuidRouteConstraint.cs
Last active December 16, 2015 12:28
RouteConstraint to ensure a parameter is a Guid
private class GuidRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
object value;
if (!values.TryGetValue(parameterName, out value))
{
return false;
}
@sebnilsson
sebnilsson / BiDictionary.cs
Created May 2, 2013 06:57
Bi-directional Dictionary with constructors to help out with initialization
public class BiDictionary<TFirst, TSecond>
{
private readonly IDictionary<TFirst, TSecond> firstToSecond;
private readonly IDictionary<TSecond, TFirst> secondToFirst;
public BiDictionary()
{
firstToSecond = new Dictionary<TFirst, TSecond>();
secondToFirst = new Dictionary<TSecond, TFirst>();
@sebnilsson
sebnilsson / EnsureRouteValueAttribute.cs
Created May 24, 2013 13:28
MVC-attribute to ensure certain route-value
public class EnsureRouteValueAttribute : ActionFilterAttribute
{
private readonly bool ignoreCase;
private readonly string routeValueKey;
private readonly string ensureRouteValue;
public EnsureRouteValueAttribute(string routeValueKey, string ensureRouteValue, bool ignoreCase = true)
{
@sebnilsson
sebnilsson / choco-pack-install.ps1
Last active July 18, 2016 10:24
Build and test Chocolatey-package
choco pack
choco install {PACKAGE_NAME} -s "$pwd" -f # -y
@sebnilsson
sebnilsson / Crc32.cs
Created June 4, 2013 06:42
Crc32 in C#, with more .NET-like syntax than the original
// Based on:
// https://github.com/rbrian/mccszlib/blob/master/Crc32.cs
public static class Crc32
{
private const uint CrcSeed = 0xFFFFFFFF;
#region CRC Table
private static readonly uint[] CrcTable = new uint[]
{
@sebnilsson
sebnilsson / DiskFileDependencyHelper.cs
Created June 10, 2013 09:12
Disk-file dependency helper for restarting ASP.NET Environment on disk-file change
public static class DiskFileDependencyHelper
{
public static void AddRestartWatch(string filePath)
{
var fileInfo = new FileInfo(filePath);
if (fileInfo.Directory == null)
{
throw new FileNotFoundException("File or folder not found.", filePath);
}
@sebnilsson
sebnilsson / RequestLazy.cs
Last active December 18, 2015 07:39
Lazy init objects stored in the current ASP.NET-request.
public class RequestLazy<T>
{
private readonly Func<T> valueFactory;
private readonly Func<IDictionary> storeFactory;
internal RequestLazy(Func<T> valueFactory, string storeKey, Func<IDictionary> storeFactory)
{
this.storeFactory = storeFactory ?? (() => (HttpContext.Current != null) ? HttpContext.Current.Items : null);
this.valueFactory = valueFactory ?? this.GetValue;
@sebnilsson
sebnilsson / Duplicates.cs
Last active December 18, 2015 14:19
Get duplicates with LINQ
public static IEnumerable<TSource> Duplicates<TSource>(this IEnumerable<TSource> source)
{
return from grouping in source.GroupBy(x => x) where grouping.Count() > 1 from item in grouping select item;
}
public static IEnumerable<TSource> Duplicates<TSource>(
this IEnumerable<TSource> source, Func<TSource, object> keySelector)
{
return from grouping in source.GroupBy(keySelector)
where grouping.Count() > 1
@sebnilsson
sebnilsson / GetAssemblyTypes.cs
Last active December 18, 2015 16:39
Helper to get types assignable from specific type, with reflection.
public static IEnumerable<Type> GetAssemblyTypes<TType>(params Assembly[] assemblies)
{
return GetAssemblyTypes(typeof(TType), assemblies);
}
public static IEnumerable<Type> GetAssemblyTypes(Type rootType, params Assembly[] assemblies)
{
if (assemblies == null || !assemblies.Any())
{
assemblies = new[] { rootType.Assembly };