Skip to content

Instantly share code, notes, and snippets.

@danielcrenna
danielcrenna / DynamicJson.cs
Created September 14, 2012 03:22
Dynamic wrapper around a JSON serializer (in this case, ServiceStack)
using ServiceStack.Text;
public class DynamicJson : DynamicObject
{
private readonly IDictionary<string, object> _hash = new Dictionary<string, object>();
public static string Serialize(dynamic instance)
{
var json = JsonSerializer.SerializeToString(instance);
return json;
@danielcrenna
danielcrenna / XmlFormatter
Created October 2, 2012 20:15
Format XML media as plainly as possible
public class XmlFormatter : MediaTypeFormatter
{
private static readonly IDictionary<Type, XmlSerializer> Cache;
private static readonly XmlSerializerNamespaces IgnoreNamespaces;
static XmlFormatter()
{
Cache = new Dictionary<Type, XmlSerializer>();
IgnoreNamespaces = new XmlSerializerNamespaces();
IgnoreNamespaces.Add("", "");
@danielcrenna
danielcrenna / BubbleSort.cs
Created December 15, 2012 05:24
An implementation of bubble sort in C#.
using System.Collections.Generic;
public class BubbleSort
{
public void Sort<T>(IList<T> items)
{
var due = true;
while(due)
{
due = false;
@danielcrenna
danielcrenna / InsertionSort.cs
Created December 18, 2012 04:35
An implementation of insertion sort in C#.
using System.Collections.Generic;
public class InsertionSort
{
public void Sort<T>(IList<T> items)
{
for (var i = 1; i < items.Count; i++)
{
int j;
var left = items[i];
@danielcrenna
danielcrenna / Program.cs
Created May 14, 2015 16:49
.NET / Node AES round-trip
using System;
using System.Security.Cryptography;
using System.Text;
namespace Aes
{
static class Program
{
static void Main()
{
@danielcrenna
danielcrenna / JsonContractResolver.cs
Created October 21, 2015 16:14
JSON.NET polyfill
/// <summary>
/// Handles casing, as well as conventionally deserializes to any interfaces rather than the full type
/// License: https://opensource.org/licenses/MIT
/// </summary>
public class JsonContractResolver : DefaultContractResolver
{
private readonly Case _case;
public enum Case
{
@danielcrenna
danielcrenna / NoContainer.cs
Last active January 13, 2018 01:27
Super small (one file), super fast (dynamic IL), replacement for OPDI (other people's dependency injectors)
#region License
/*
NoContainer - Because, no.
--------------------------
Copyright(c) 2016-2017 Daniel Crenna
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
@danielcrenna
danielcrenna / ProfilingDbCommand.cs
Last active June 21, 2017 15:54
IDbConnection profiling in .NET Core
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace DatabaseProfiling
{
@danielcrenna
danielcrenna / Add.cs
Last active June 2, 2017 17:15
Swagger support in .NET Core + Azure
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
namespace SwaggerFeature
{
public static class Add
@danielcrenna
danielcrenna / GitTools.cs
Last active October 16, 2018 17:56
parse git hash out of an embedded resource or existing git DB
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace DevOps
{
public class GitTools
{
public static string GetGitHash(string @namespace)