Skip to content

Instantly share code, notes, and snippets.

Working from home

Vince badmotorfinger

Working from home
View GitHub Profile
badmotorfinger / ClassGenerator.sql
Created May 11, 2018 — forked from ernado-x/ClassGenerator.sql
Generate C# class from database table
View ClassGenerator.sql
--from SO:
declare @TableName sysname = 'TableName'
declare @Result varchar(max) = 'public class ' + @TableName + '
select @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
badmotorfinger / dupfinder.ps1
Last active Mar 8, 2020
Use ReSharper command line tool dupFinder to produce a report of duplicated C# code
View dupfinder.ps1
$reportFileName = 'dupfinder.html'
$dupFindLocation = 'C:\dev\tools\ReSharperCliTools\dupfinder.exe'
if (-not (Test-Path $dupFindLocation)) {
Write-Host "dupfinder.exe not found in path $dupFindLocation"
Write-Host "Download tools from"
$xsl = '<?xml version="1.0" encoding="utf-8"?>
badmotorfinger / Enable-CSharp-WarningsAsErrors.ps1
Last active Jun 15, 2017
Recursively processes all csproj files in a directory and adds/updates a node for both Release and Debug configurations to treat the specified C# warnings as errors
View Enable-CSharp-WarningsAsErrors.ps1
# Warnings to treat as errors
# See end of script for descriptions
$cSharpWarnings = '162,168,169,219,1717,0067,649,618,183,184,1060,1058,809,672,612,1522,465,1998'
$formatting = ',1570,1574'
$interopWarnings = ',626,684,824'
$casWarnings = ',688'
$warnings = $cSharpWarnings + $formatting + $interopWarnings + $casWarnings
Get-ChildItem *.csproj -Recurse | % {
badmotorfinger / json-pretty
Created Jul 29, 2014
A compact JSON beautifier
View json-pretty
// Highly compact JSON beautifier/formatter. Not very efficient and could definitely be improved. The main issue is with
// the number of allocations and memory presure being placed on the heap and garbage collector if the input is large.
// It is the shortest in terms of code size though.
// Source:
private const string INDENT_STRING = " ";
static string FormatJson(string json) {
int indentation = 0;
View NotNull
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Bleroy.Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;
badmotorfinger / SelectMany God
Last active Aug 29, 2015
Using the SelectMany() God function.
View SelectMany God
void Main()
// An implementation of the Where method using the SelectMany() method.
// An implementation of Join using the SelectMany() method.
public static IEnumerable<T> Where<T>(IEnumerable<T> stuffs, Func<T, bool> predicate)
badmotorfinger / gist:9618765
Last active Aug 29, 2015
Use LINQ query expressions without requiring IEnumerables, to compose functions.
View gist:9618765
// Full credit goes to Wes Dyer
// Explanation of how query expressions are translated in to extension method syntax.
public class Identity<T>
public T Value { get; private set; }
public Identity(T value) { this.Value = value; }
public static implicit operator Identity<T>(T value) {
return new Identity<T>(value);
View RateLimit.cs
// Only let input Observable fire every 'n' seconds at most
// but unlike Throttle, items fire immediately when they aren't
// rate-limited.
public IObservable<T> RateLimit<T>(this IObservable<T> This, TimeSpan interval, IScheduler scheduler)
var slot = default(IObservable<Unit>);
var input = This.Publish().RefCount();
return input.Window(input, _ => {
if (slot != null) return slot;
badmotorfinger / gist:5972009
Last active Dec 19, 2015
A LINQ extension method which eagerly finds an item or items with the lowest value in the collection.
View gist:5972009
public void TestEmpty()
new int[0].MinsBy(i => i),
new int[0]);
public void TestPreserveOrderAndValue()
View TestHelper.cs
public static class TestHelpers
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);