Skip to content

Instantly share code, notes, and snippets.

@smailliwcs
smailliwcs / BufferedTraceListener.cs
Created September 22, 2020 15:26
Buffering a TraceListener
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
public class BufferedTraceListener : TraceListener
{
private SynchronizationContext context;
private StringBuilder buffer;
private object bufferLock;
@smailliwcs
smailliwcs / LoggingCommand.cs
Created September 22, 2020 15:26
Logging SQL queries in ADO.NET
using System.Data;
public class LoggingCommand : IDbCommand
{
private static void Log(string message)
{
// ...
}
private IDbCommand @base;
@smailliwcs
smailliwcs / TwoWayDictionary.cs
Created September 22, 2020 15:26
Two-way dictionaries
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class TwoWayDictionary<T1, T2> : ICollection<Tuple<T1, T2>>
{
private static Tuple<T1, T2> ToTuple(T1 item1, T2 item2)
{
return Tuple.Create(item1, item2);
@smailliwcs
smailliwcs / InterlockedBoolean.cs
Created September 22, 2020 15:26
Interlocked Booleans
using System;
using System.Threading;
public class InterlockedBoolean
{
private int value;
public bool Value
{
get { return Convert.ToBoolean(value); }
@smailliwcs
smailliwcs / Base64Extensions.cs
Created September 22, 2020 15:25
Base64 serialization
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public static class Base64Extensions
{
private static IFormatter GetFormatter()
{
return new BinaryFormatter();
@smailliwcs
smailliwcs / makepfx.bat
Created September 22, 2020 15:25
Creating self-signed certificates
@echo off
setlocal
set cn=%~1
if "%cn%" == "" goto :usage
makecert -sv "%cn%.pvk" -n "CN=%cn%" -b 01/01/2000 -e 12/31/2999 -r "%cn%.cer"
pvk2pfx -pvk "%cn%.pvk" -spc "%cn%.cer" -pfx "%cn%.pfx"
goto :eof
:usage
@smailliwcs
smailliwcs / ConditionalRequiredAttribute.cs
Created September 22, 2020 15:25
Conditional required validation
using System.ComponentModel.DataAnnotations;
using System.Reflection;
public sealed class ConditionalRequiredAttribute : RequiredAttribute
{
public override bool RequiresValidationContext
{
get { return true; }
}
@smailliwcs
smailliwcs / columns.js
Created September 22, 2020 15:25
TinyMCE plugin: columns
tinymce.PluginManager.add("columns", function (editor) {
function insert(count) {
editor.undoManager.transact(function () {
var row = editor.dom.create("div", { "class": "row" });
var width = Math.ceil(12 / count);
for (var index = 0; index < count; index++) {
var column = editor.dom.add(row, "div", { "class": "col-sm-" + width });
editor.dom.add(column, "p", {}, "Column " + (index + 1));
}
var node = editor.selection.getNode();
@smailliwcs
smailliwcs / paragraphs.js
Created September 22, 2020 15:25
TinyMCE plugin: paragraphs
tinymce.PluginManager.add("paragraphs", function (editor) {
function insert(place) {
var body = editor.getBody();
var paragraph = editor.dom.create("p");
place(paragraph, body);
editor.selection.setCursorLocation(paragraph);
editor.nodeChanged();
}
editor.addMenuItem("paragraph", {
@smailliwcs
smailliwcs / glyphicons.js
Created September 22, 2020 15:25
TinyMCE plugin: glyphicons
tinymce.PluginManager.add("glyphicons", function (editor) {
function insert(name) {
editor.insertContent("<span class='glyphicon glyphicon-" + name + "'></span>");
}
function getMenuItem(name) {
return {
text: name,
onclick: function () {
insert(name);