Skip to content

Instantly share code, notes, and snippets.

View dadhi's full-sized avatar
🎯
Focusing

Maksim Volkau dadhi

🎯
Focusing
View GitHub Profile
@dadhi
dadhi / TypeGetGenArgsVsTypeInfoGenTypeParsArgs.cs
Last active October 20, 2015 14:28
Compare Type.GetGenericArguments() with new reflection TypeInfo.GenericType[Arguments|Parameters]
public class D<T0, T1> { }
public class G<T0, T1> : D<int, T1> { }
[Test]
public void Get_generic_arguments_should_work_the_same_on_all_platforms()
{
// For generic type definition Type.GetGenArgs == TypeInfo.GenPars
var typeGenArgs = typeof(G<,>).GetGenericArguments();
var infoGenArgs = typeof(G<,>).GetTypeInfo().GenericTypeArguments;
var infoGenPars = typeof(G<,>).GetTypeInfo().GenericTypeParameters;
@dadhi
dadhi / ExtractNamesFromDelegate.cs
Created October 7, 2015 08:05
Extracts field, property, event or method name(s) from provided delegate using IL analysis
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
namespace DryTools
{
public static class ExtractName
{
@dadhi
dadhi / DryIoc_Issue164_EventAggregator.cs
Created September 17, 2015 16:22
DryIoc_Issue164_EventAggregator
using System;
using NUnit.Framework;
namespace DryIoc.IssuesTests
{
[TestFixture]
public class Issue164_EventAggregatorImpl
{
[Test]
public void Able_to_handle_multiple_events_being_singleton()
@dadhi
dadhi / HashTree.cs
Created May 15, 2015 12:36
Immutable and very fast AVL based HashTree in C#
/// <summary>Delegate for changing value from old one to some new based on provided new value.</summary>
/// <typeparam name="V">Type of values.</typeparam>
/// <param name="oldValue">Existing value.</param>
/// <param name="newValue">New value passed to Update.. method.</param>
/// <returns>Changed value.</returns>
public delegate V Update<V>(V oldValue, V newValue);
/// <summary>Immutable http://en.wikipedia.org/wiki/AVL_tree where actual node key is hash code of <typeparamref name="K"/>.</summary>
/// <remarks>Does not support Remove, though it easy to implement based on Eric Lippert's http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx.
/// You may emulate Remove by updating key value to null.
@dadhi
dadhi / Either.cs
Last active April 12, 2016 10:04
Take on minimal yet extensible approach to implementing Either/Or/DiscriminatedUnion
using System;
using System.Reflection;
using NUnit.Framework;
namespace Playground
{
[TestFixture]
public class EitherTests
{
[Test]
@dadhi
dadhi / gist:109b84ecb7dffe29f9bc
Last active May 14, 2021 13:43
Token-replacement template engine for PowerShell
<#
Token-replacement template engine for PowerShell courtesy to http://www.bricelam.net/2012/09/simple-template-engine-for-powershell.html
Usage:
Merge-Tokens 'Hello, $target$! My name is $self$.' @{ target = 'World'; self = 'Brice' }
#>
function Merge-Tokens($template, $tokens)
{
return [regex]::Replace($template, '\$(?<token>\w+)\$',
{ param($match) $tokens[$match.Groups['token'].Value] })
@dadhi
dadhi / increment-count.bat
Created June 17, 2014 06:14
Batch script for persistent counter. Could be used for implementing state machine or suffixing backup files, etc. Uses "count file" to store counter between script executions.
@echo off
if [%1]==[] (echo error: Please specify countfile as command line argument. Could be count.txt or similar. && exit /B 1)
set COUNTFILE=%1
(set /P COUNT=<%COUNTFILE%)2>nul || set COUNT=0
set /A COUNT+=1
echo:%COUNT%>%COUNTFILE%
@dadhi
dadhi / DiscriminatedUnion3.cs
Created May 28, 2014 10:32
Liked this implementation of F# Discriminated Union in C# - http://stackoverflow.com/a/3199453/2492669
using System;
namespace Juliet
{
class Program
{
static void Main(string[] args)
{
Union3<int, char, string>[] unions = new Union3<int,char,string>[]
{
@dadhi
dadhi / MultimethodsWithDynamicTests
Last active January 13, 2016 10:48
Proof of concept and very simple Multimethods implementation, that uses dynamic keyword from .Net 4.0. Thanks goes to <http://blogs.msdn.com/b/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx>.
using NUnit.Framework;
using Microsoft.CSharp.RuntimeBinder;
namespace MultiMethodsWithDymanic
{
[TestFixture]
public class MultimethodsTests
{
[Test]
public void What_is_good_for_rabbit_is_not_good_for_wolf()
using System;
using System.Linq;
using System.Threading;
using NUnit.Framework;
namespace Playground
{
[TestFixture]
public class RefTests
{