Skip to content

Instantly share code, notes, and snippets.

@dlumpp
dlumpp / ShouldHavePropertyValuesEqualTo.cs
Last active July 9, 2018 16:49
[Shouldy Deep Compare] A Shouldly extension to perform a recursive, memberwise compare of two object's public property values #testing #Shouldly
using System;
using System.Collections;
namespace Shouldly
{
static class ShouldlyExtensions
{
public static void ShouldHavePropertyValuesEqualTo(this object actual, object expected)
{
foreach (var prop in expected.GetType().GetProperties())
@dlumpp
dlumpp / BetterJsonResult.cs
Last active July 9, 2018 16:48
[BetterJsonResult] An MVC JsonResult using Newtonsoft serialization to create friendlier JSON #MVC #JSON
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Web;
using System.Web.Mvc;
using System;
namespace MyMvcApp.ActionResults
{
public class BetterJsonResult : JsonResult
@dlumpp
dlumpp / ExecutionTimer.cs
Last active July 9, 2018 16:45
[ExecutionTimer] A code timer using an IDisposable wrapper around a Stopwatch that allows you to add a using statement above the statement you want to time.
using System;
using System.Diagnostics;
namespace Diagnostics
{
public class ExecutionTimer : IDisposable
{
private Stopwatch stopwatch;
private string name;
private Action<string> outputAction;
@dlumpp
dlumpp / Count-Child-Items.ps1
Last active July 9, 2018 16:53
[Count Files] Count all files and folders in a folder #filesystems
Write-Host ( Get-ChildItem C:\somefolder | Measure-Object ).Count
@dlumpp
dlumpp / Move-Many-Patterns.ps1
Created July 10, 2018 18:44
[Move Files by Multiple Patterns] Move all files matching any of an array of wildcard patterns #filesystem
# Moves only files matching any wildcard in the array
# PowerShell has a bug that throws errors for each non-matching file
# but it still works and moves the matches
# https://github.com/PowerShell/PowerShell/issues/2385
$filters = @("*.pdf", "*.jpg")
# alternatively read in filters from a text file, separated by newlines
# $filterFile = "C:\filters.txt"
# $filters = Get-Content -Path $filterFile
@dlumpp
dlumpp / IIndexAccessible.cs
Created August 24, 2018 16:10
[IIndexAccessible] Indexed property interface that I wish was part of .NET so that Dictionary could implement it
public interface IIndexAccessible<TKey, TValue>
{
TValue this[TKey index] { get; }
}
@dlumpp
dlumpp / SurroundWithParentheses.snippet
Created October 25, 2019 15:36
Surround With Parentheses - Visual Studio Surrounds With Snippet
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>parens</Title>
<Shortcut>parens</Shortcut>
<Description>Code snippet to surround a block of code with parentheses</Description>
<Author>Dan Lumpp</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
@dlumpp
dlumpp / EmbeddedResources.cs
Created February 1, 2021 14:32
[Read Embedded Resources] Read string resources embedded in assembly #testing
using System;
using System.IO;
using System.Reflection;
public static class EmbeddedResources
{
public static string Read(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
var defaultNamespace = assembly.GetName().Name;
@dlumpp
dlumpp / CalculatorTestTheoryData.cs
Created February 12, 2021 18:11
[xUnit TheoryData] Strongly typed theory test data #testing #xunit
// https://andrewlock.net/creating-strongly-typed-xunit-theory-test-data-with-theorydata/
public class CalculatorTestData : TheoryData<int, int, int>
{
public CalculatorTestData()
{
Add(1, 2, 3);
Add(-4, -6, -10);
Add(-2, 2, 0);
Add(int.MinValue, -1, int.MaxValue);
Add(1.5, 2.3m, "The value"); // will not compile!
@dlumpp
dlumpp / ListHelper.cs
Created May 18, 2022 14:11
Simpler syntax for creating single-item lists
using System.Linq;
namespace System.Collections.Generic;
public static class List
{
public static List<T> From<T>(T value) => new(1) { value };
public static List<T> From<T>(params T[] values) => values.ToList();
}