Skip to content

Instantly share code, notes, and snippets.

View stakx's full-sized avatar

Dominique Schuppli stakx

  • Switzerland
View GitHub Profile
@stakx
stakx / AsyncInterceptor.cs
Last active January 10, 2021 22:06
AsyncInterceptor for Castle DynamicProxy, assuming that IInvocation.GetProceedInfo() is available. Doesn't yet support exceptions.
public abstract class AsyncInterceptor : IInterceptor
{
private static readonly MethodInfo transitionMethod =
typeof(AsyncInterceptor).GetMethod(nameof(TransitionToAsync), BindingFlags.NonPublic | BindingFlags.Instance);
protected AsyncInterceptor()
{
}
void IInterceptor.Intercept(IInvocation invocation)
@stakx
stakx / IgnoresAccessChecksToAttributeDemo.cs
Last active July 20, 2018 08:15
Demonstrates the special `IgnoresAccessChecksToAttribute` of .NET 4.6+ and .NET Core 1.0+
using System;
using System.Reflection;
using System.Reflection.Emit;
// Note that this is `internal` and regularly shouldn't be accessible from other assemblies:
internal sealed class Hellower
{
internal void SayHello()
{
Console.WriteLine("Hello!");
@stakx
stakx / index.html
Created February 14, 2017 11:03
Demonstrates a bug with `md-truncate` in `angular-material` versions 1.1.2 and 1.1.3
<!DOCTYPE html>
<html>
<head>
<script src="node_modules/angular/angular.min.js"></script>
<script src="node_modules/angular-animate/angular-animate.min.js"></script>
<script src="node_modules/angular-aria/angular-aria.min.js"></script>
<script src="node_modules/angular-material/angular-material.min.js"></script>
<link rel="stylesheet" href="node_modules/angular-material/angular-material.min.css">
</head>
<body ng-app="app">
@stakx
stakx / App.config
Last active October 24, 2016 21:42
XmlResolverSandbox
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="SqlServerDatabase" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SqlServerDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
@stakx
stakx / ObservableObject.cs
Last active August 29, 2015 14:17
[CallerMemberName]-based implementation of INotifyPropertyChanged
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected ObservableObject()
@stakx
stakx / ITable.SearchAsEnumerable.cs
Last active May 4, 2017 16:42
Demonstrates how ArcObjects cursors can be transformed to `foreach`-able sequences.
// using ESRI.ArcGIS.Geodatabase;
// using System;
// using System.Collections.Generic;
// using System.Runtime.InteropServices;
public static IEnumerable<IRow> SearchAsEnumerable(this ITable table, IQueryFilter queryFilter, bool recycling)
{
Func<ICursor> createCursor = () => table.Search(queryFilter, recycling);
return createCursor.AsEnumerable();
}
@stakx
stakx / Enumerable.Pairwise.cs
Created October 18, 2014 18:39
LINQ operator that maps over adjacent items in the source sequence using the given selector function
static IEnumerable<R> Pairwise<T, R>(this IEnumerable<T> xs, Func<T, T, R> selector)
{
using (var enumerator = xs.GetEnumerator())
{
if (enumerator.MoveNext())
{
T x = enumerator.Current;
while (enumerator.MoveNext())
{
T y = enumerator.Current;
@stakx
stakx / ContainerBuilder.cs
Last active December 25, 2015 00:59
The simplest, minimally useful non-auto-wiring Dependency Injection container (inspired by Autofac) that I could think of in about half a day's time.
using System;
using System.Collections.Generic;
public sealed class ContainerBuilder
{
public ContainerBuilder()
{
this.registrations = new Dictionary<RuntimeTypeHandle, object>();
}
// Dummy definition of the binary tree:
type Deque<'a> =
| Empty
| Node of Deque<'a> * 'a * Deque<'a>
// Traverse from "front" (L) to "back" (R):
let rec items D =
match D with
| Empty -> Seq.empty
| Node(L,x,R) -> seq {
@stakx
stakx / Deque+Empty.cs
Last active September 5, 2015 10:44
(Naïve) implementation of immutable double-ended queues (deques) based on balanced binary trees.
using System;
partial class Deque<T>
{
private sealed class Empty<T> : IDeque<T>
{
public IDeque<T> PushFront(T value)
{
return new Deque<T>(value, Deque<T>.Empty, Deque<T>.Empty);
}