Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / Tuples.tt
Last active December 21, 2015 00:09
Create as many n-tuples as you like
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ output extension=".Designer.cs" #>
using System.Collections.Generic;
namespace <#=CallContext.GetData("NamespaceHint")#>
{

TypeScript (0.9.1)

The Language

Type Checks

Primitives

A primitive type is a number, bool, string or void.

Before type check:

@robfe
robfe / Xunit.cs
Created October 1, 2013 21:37
Hack a Fact subclass into your current project to make specflow skip any tests with pending steps. Ultra dirty/unfinished
extern alias rxu; //alias the xunit dll to "rxu" - Real X Unit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using TechTalk.SpecFlow;
namespace Xunit
{
public class FactAttribute:rxu.Xunit.FactAttribute
@robfe
robfe / ObservableCollectionExtensions.cs
Created October 7, 2013 22:51
[RX] A live selectmany for items currently inside an observable collection
public static IObservable<TResult> SelectMany<TSource, TResult>(this ObservableCollection<TSource> collection, Func<TSource, IObservable<TResult>> selector)
{
return Observable.Create<TResult>(observer =>
{
// assume events are raised on the same thread (i.e. dispatcher), so we don't need a concurrent dictionary
var subscriptions = new Dictionary<TSource, IDisposable>();
foreach (var source in collection)
{
//subscribe selector to all items already in the collection