Skip to content

Instantly share code, notes, and snippets.

@robfe
robfe / StepArgumentTransforms.cs
Created July 23, 2013 04:17
string to boolean transforms for specflow step names
[Binding]
public class StepArgumentTransforms
{
static readonly List<string> _positives = "is,can,will,should,does,do,have,correct".Split(',').ToList();
static readonly List<string> _negatives = "{0}n't,{0}not,{0} not,{0} no,in{0},un{0}".Split(',').SelectMany(s => _positives.Select(word => string.Format(s, word))).ToList();
[StepArgumentTransformation]
public bool EnglishBool(string s)
{
if (_positives.Contains(s))
@robfe
robfe / Typescript.md
Last active December 16, 2015 20:29

TypeScript (0.8)

  • A superset of the JavaScript language.
  • Code in TypeScript, compile to JavaScript, deploy JavaScript. (Similar workflow to CoffeeScript)

Advantages

  • Very similar to JavaScript.
  • Type safety - when you need it. TypeScript is a gradually typed language.
  • Less boilerplate.
  • Programmer's intent is clearer in the code.
@robfe
robfe / EnumDescriptionMapper.cs
Created April 9, 2013 02:54
reads System.ComponentModel.DescriptionAttributes for each value in an enum, and provides two-way mapping.
static class EnumDescriptionMapper<T>
{
public static readonly Dictionary<T, string> EnumDescriptions;
public static readonly Dictionary<string, T> DescriptionsValues;
static EnumDescriptionMapper()
{
var values = (T[])Enum.GetValues(typeof(T));
EnumDescriptions = values.ToDictionary(x => x, GetAttributeDescription);
DescriptionsValues = EnumDescriptions.ToDictionary(x => x.Value, x => x.Key);
@robfe
robfe / Extensions.cs
Created April 4, 2013 19:53
Dictionary GetOrCreate
public static class Extensions
{
public static TValue GetOrCreate<TKey, TValue>(this Dictionary<TKey, TValue> d, TKey key, Func<TKey, TValue> valueFactory)
{
TValue value;
if (!d.TryGetValue(key, out value))
{
d[key] = value = valueFactory(key);
}
return value;
@robfe
robfe / Hubs.tt
Last active March 18, 2020 16:17
T4 template that creates Typescript type definitions for all your Signalr hubs. If you have C# interface named "I<hubName>Client", a TS interface will be generated for the hub's client too.If you turn on XML documentation in your build, XMLDoc comments will be picked up.Licensed with http://www.apache.org/licenses/LICENSE-2.0
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".d.ts" #>
<# /* Update this line to match your version of SignalR */ #>
<#@ assembly name="$(SolutionDir)\packages\Microsoft.AspNet.SignalR.Core.2.2.0\lib\net45\Microsoft.AspNet.SignalR.Core.dll" #>
<# /* Load the current project's DLL to make sure the DefaultHubManager can find things */ #>
<#@ assembly name="$(TargetPath)" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web" #>
<#@ assembly name="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #>
<#@ assembly name="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #>
@robfe
robfe / TileCanvas.cs
Created July 9, 2012 17:48
TileCanvas in WPF
public class TileCanvas : Canvas
{
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(TileCanvas), new UIPropertyMetadata(null, OnImageSourceChanged));
private static void OnImageSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
((TileCanvas)o).Rebuild();
}
public ImageSource ImageSource
@robfe
robfe / TileCanvas.cs
Created June 27, 2012 21:05
Tilebrush equivalent for WinRT XAML apps.
public class TileCanvas : Canvas
{
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(TileCanvas), new PropertyMetadata(null, ImageSourceChanged));
private Size lastActualSize;
public TileCanvas()
{
LayoutUpdated += OnLayoutUpdated;
}
@robfe
robfe / SquaresToPolygonGenerator.cs
Created May 10, 2012 09:28
Takes a 2d array of bools (representing squares on a grid) and gives you the polygon that wraps those squares
/// <summary>
/// C# port of http://raksy.dyndns.org/cycle_collection.py via http://stackoverflow.com/questions/8946563/add-square-to-polygon-composed-of-squares
/// </summary>
static class SquaresToPolygonGenerator
{
static readonly Coordinate[] directions = new[]
{
Coordinate.West,
Coordinate.South,
Coordinate.East,
@robfe
robfe / AppBarButtons.tt
Created January 8, 2012 23:46
t4 template for strong access to all your AppBarButtons in a wp7 Page
<#@ template language="C#" hostspecific="True" debug="false" #>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Xml.Linq"#>
<#@ import namespace="System.Collections.Generic"#>
<#@ import namespace="System.Runtime.Remoting.Messaging"#>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating"#>
<#@ assembly name="System.Core.dll"#>
<#@ assembly name="System.Xml.Linq.dll"#>
<#@ assembly name="System.Xml.dll"#>
@robfe
robfe / ApiDocumentor.cs
Created November 16, 2011 11:41
Documents the webget and webinvoke methods of a ServiceContract
[TestClass]
public class ApiDocumentor
{
[TestMethod]
public void PrintApiMethods()
{
var methods = from m in typeof(Api).GetMethods()
let url = GetUrl(m)
where url != null