Skip to content

Instantly share code, notes, and snippets.

View jchadwick's full-sized avatar

Jess Chadwick jchadwick

View GitHub Profile
@jchadwick
jchadwick / gist:6393258
Created August 30, 2013 19:08
Entity Framework type conversion
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
public class Animal
{
public long Id { get; set; }
}
public class Dog : Animal
@jchadwick
jchadwick / Parser.cs
Last active December 19, 2015 07:39
Parser
using System;
using System.Collections.Generic;
using System.Linq;
public static class Parser
{
public static readonly IDictionary<Type, Func<string, object>> Parsers =
new Dictionary<Type, Func<string, object>>
{
{typeof (char), source => char.Parse(source)},
@jchadwick
jchadwick / MainWindow.xaml
Created March 7, 2013 03:28
WPF Countdown Timer
<Window x:Class="StoryboardDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock x:Name="CountdownDisplay" FontFamily="Calibri" FontSize="60"/>
</Grid>
</Window>
@jchadwick
jchadwick / DynamicSqlDataReader.cs
Created September 20, 2012 13:18
A helper class to read SQL results into a dynamic object
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
public class DynamicSqlDataReader
{
private static dynamic ToExpando(IDataRecord record)
{
@jchadwick
jchadwick / QueuedService.cs
Last active January 11, 2019 15:03
A helper class that provides delayed processing of a message via MSMQ
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Messaging;
using NLog;
/// <summary>
/// A facade over Microsoft's MSMQ
/// </summary>
public abstract class QueuedService : IDisposable
@jchadwick
jchadwick / gist:2956175
Created June 19, 2012 19:55
Get Project path for VS Test Runner
// Yeah, this is crazy, right? Stupid Visual Studio test runner...
var testPathReplacer = new System.Text.RegularExpressions.Regex(@"\\(?:(TestResults\\[^\\]*\\Out)|([^\\]*\\bin\\[^\\]*))");
var assemblyDirectory = Path.GetDirectoryName(GetType().Assembly.Location);
assemblyDirectory = testPathReplacer.Replace(assemblyDirectory, string.Empty);
assemblyDirectory = Path.Combine(assemblyDirectory, PROJECT_DIRECTORY);
@jchadwick
jchadwick / gist:2889702
Created June 7, 2012 16:09
Promote XElement to attribute
private static void PromoteElementToAttribute(XElement element, string xname)
{
var names = element.Descendants(xname).ToArray();
foreach(var name in names)
{
if (name != null && name.Parent != null)
{
name.Parent.SetAttributeValue(xname, name.Value);
name.Remove();
}
@jchadwick
jchadwick / MarkdownRazorViewEngine.cs
Created May 25, 2012 18:25
ViewEngine with Markdown and Razor support
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using MarkdownDeep;
public class MarkdownRazorViewEngine : RazorViewEngine
{
@jchadwick
jchadwick / InMemoryAspNet.cs
Created May 16, 2012 19:24
In-Memory ASP.NET Website
using System;
using System.Web;
using System.Web.Hosting;
using System.IO;
public class InMemoryAspNet
{
public string PhysicalDirectory { get; set; }
public InMemoryAspNet()
@jchadwick
jchadwick / ControlCommand.cs
Created April 20, 2012 19:11
Facade for Cross-Queue MSMQ communication
public struct ControlCommand
{
public static readonly ControlCommand Message = 0;
private readonly int _value;
public ControlCommand(int value)
{
_value = value;
}