Skip to content

Instantly share code, notes, and snippets.

@ashmind
ashmind / run-cli.csproj
Last active Oct 13, 2021
Arbitrary scripts with dotnet run
View run-cli.csproj
<Project>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<OutDir>build/bin</OutDir>
<BaseIntermediateOutputPath>build/obj</BaseIntermediateOutputPath>
</PropertyGroup>
<Target Name="PrepareProgram" BeforeTargets="BeforeBuild">
<PropertyGroup>
@ashmind
ashmind / OwinWebSocket.cs
Created Sep 14, 2016
(Very flawed) Implementation of System.Net.WebSockets.WebSocket over Owin async func spec
View OwinWebSocket.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Threading;
// This is bad and potentially unreliable code, which only serves as a base for
// further improvements. Please do not use it as is.
namespace MirrorSharp.Owin.Internal {
View AutofacCompositeExtensionsPrototype.cs
public static class AutofacCompositeExtensions {
public static IRegistrationBuilder<TImplementer, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterComposite<TImplementer>(this ContainerBuilder builder) {
var registrationBuilder = RegistrationBuilder.ForType<TImplementer>();
builder.RegisterCallback(registry => {
registrationBuilder.OnPreparing(e => {
foreach (var service in e.Component.Services.OfType<IServiceWithType>()) {
e.Parameters = e.Parameters.Concat(new[] {
new ResolvedParameter(
(p, _) => IsCollectionOf(p.ParameterType, service),
(_, c) => ResolveCollection(c, service, e.Component)
View Update-FromGitHubUpstream.ps1
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
function Write-ColorOutput([Parameter(Mandatory=$true)] $inputObject, [System.ConsoleColor] $foregroundColor) {
$rawUI = $Host.UI.RawUI
$saved = $rawUI.ForegroundColor
try {
$rawUI.ForegroundColor = $foregroundColor
Write-Output $inputObject
}
View TortoiseSVN+P4Merge.md
Diff:
C:\Program Files\Perforce\p4merge.exe -nl %bname -nr %yname %base %mine

Merge:
C:\Program Files\Perforce\p4merge.exe -nb %bname -nl %tname -nr %yname -nm %mname %base %theirs %mine %merged
View CSharp6.md

String interpolation

var x = 1;

// same as string.Format("A {0} B", x) 
var y1 = $"A {x} B"; // y == "A 1 B"

// $@ for multiline
var y2 = $@"A
View AssertExtensibility.cs
using System;
namespace SomeUnit {
public class AssertHelper {
public static AssertHelper Assert { get; } = new AssertHelper();
}
}
namespace SomeUnit.Extensions {
public static class AssertExtensions {
@ashmind
ashmind / ContractResolverWithPatchSupport.cs
Last active Aug 29, 2015
WebApi Patch<T> Design (at the moment needs AshMind.Extensions and InfoOf from NuGet)
View ContractResolverWithPatchSupport.cs
using System;
using System.Collections.Generic;
using System.Linq;
using AshMind.Extensions;
using Newtonsoft.Json.Serialization;
namespace PatchDesignDemo {
public class ContractResolverWithPatchSupport : DefaultContractResolver {
private readonly IContractResolver _inner;
View ExpressionProblem
public class Node {}
public class NodeA : Node {}
public class NodeB : Node {}
public class NodeBProcessor : Processor<NodeB> {
}
public class ProcessingDispatcher {
public void Dispatch(Node node) {
var processor = FindBestProcessorFor(node);
@ashmind
ashmind / NuGet_profile.ps1
Last active Sep 29, 2018
Save to your NuGet Profile
View NuGet_profile.ps1
# For each package, lists which projects it is installed in
function Write-PackageProjectInstalls([string] $PackageFilter = '') {
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
Get-Project -All |
% {
$ProjectName = $_.ProjectName
Get-Package $PackageFilter -Project $ProjectName |
% { New-Object PSObject -Prop (@{ ProjectName=$ProjectName; PackageId=$_.Id; Version = $_.Version; }) }