Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
NickStrupat / AutoPropertyExtensions.cs
Last active March 2, 2023 12:46
Get the backing field of an auto-property and vice versa (this only works for auto-props generated by C# compiler, and is not as good as Mono.Reflection.GetBackingField)
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
public static class AutoPropertyExtensions {
const String Prefix = "<";
const String Suffix = ">k__BackingField";
private static String GetBackingFieldName(String propertyName) => $"{Prefix}{propertyName}{Suffix}";
#pragma once
#if defined(_WIN32)
# include <Windows.h>
#elif defined(__APPLE__) || defined(LINUX)
# include <sys/mman.h>
#endif
#include <optional>
#include "result.hpp"
@NickStrupat
NickStrupat / InterfaceImplementor.cs
Created August 28, 2021 21:26
Pass an interface type and receive an instance of a run-time-emitted class which implements the interface.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
public static class InterfaceImplementor
using System;
using System.Threading;
static class AsyncProgram
{
private static readonly CancellationTokenSource cts = new CancellationTokenSource();
public static CancellationToken CancellationToken => cts.Token;
static AsyncProgram()
{
@NickStrupat
NickStrupat / ExpandoObjectExtensions.cs
Last active October 13, 2020 19:16
Extend ExpandoObject to control assignment by hooking INotifyPropertyChanged
public static class Extensions
{
public static ExpandoObject WithAutoCorrect(this ExpandoObject eo)
{
(eo as INotifyPropertyChanged).PropertyChanged += Handler;
return eo;
static void Handler(object sender, PropertyChangedEventArgs e)
{
var dict = (IDictionary<String, Object>)sender;
@NickStrupat
NickStrupat / DictionaryObjectExtensions.cs
Last active October 9, 2020 16:51
Dictionary to object
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
public static class DictionaryObjectExtensions
{
@NickStrupat
NickStrupat / StrupHash.cs
Created October 15, 2019 21:56
pinning byte[] before page alignment calculation
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StrupHash
{
class Program
{
static unsafe void Main(string[] args)
class MapWrapper implements Map<string, any> {
constructor(readonly object: any) {}
clear(): void {
for (let key of this.keys())
delete this.object[key];
}
delete(key: string): boolean {
if (!this.has(key))
return false;
@NickStrupat
NickStrupat / crypto.ts
Created September 17, 2019 19:30
Small class for generating public-private key pairs, signing, and verifying signatures
import { sign } from "tweetnacl"
import { decodeUTF8 } from "tweetnacl-util";
export class Crypto {
private constructor() {}
static generateKeys(): CryptoKeyPair {
const naclKeyPair = sign.keyPair();
return new CryptoKeyPair(naclKeyPair.publicKey, naclKeyPair.secretKey);
}
@NickStrupat
NickStrupat / get-all-key-value-pairs.ts
Created September 17, 2019 18:37
Get an object as iterable pairs of eval-able keys and values
function * getAllKeyValuePairs(o: any, key: string): IterableIterator<[string, string]> {
if (o instanceof Map) {
for (let element of o.entries())
yield * getAllKeyValuePairs(element[1], `${key}.get("${element[0]}")`);
}
else if (o instanceof Date) {
for (let languageCode of ['en', 'fr'])
yield * getAllKeyValuePairs(o.toLocaleString(languageCode), `${key}.toLocaleString("${languageCode}")`);
}
else if (o instanceof Array) {