Skip to content

Instantly share code, notes, and snippets.

View jnm2's full-sized avatar
🧬
Evolving

Joseph Musser jnm2

🧬
Evolving
View GitHub Profile
@jnm2
jnm2 / LICENSE.txt
Last active April 28, 2024 19:07
StreamingZipReader proof of concept for https://github.com/dotnet/runtime/issues/59027
MIT License
Copyright (c) 2021 Joseph Musser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
declare @schemaName sysname = 'dbo'
declare @tableName sysname = 'ItemPrices'
declare @rowCondition nvarchar(max) = 'PriceCode in (''SEEDS'', ''YEAREND'')'
declare @builder nvarchar(max);
select @builder = coalesce(@builder + '
', '') + 'if exists(
select *' + QueryWithoutSelect + ')
@jnm2
jnm2 / ExpressionComparer.cs
Created June 16, 2015 18:06
Compares two abstract syntax trees.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
public sealed class ExpressionComparer : IEqualityComparer<Expression>
{
public NameComparison CompareLambdaNames { get; set; }
public NameComparison CompareParameterNames { get; set; }
using System;
using System.Diagnostics;
using System.Numerics;
#if NETCOREAPP2_1_OR_GREATER
using System.Buffers.Binary;
#endif
#nullable enable
@jnm2
jnm2 / Open types in nameof.md
Last active October 8, 2023 20:54
Open types in nameof

Open types in nameof

[TODO: follow standard sections, add examples, flesh everything out]

Motivation

This is a small feature that removes a common frustration: why do I have to pick a generic type argument when the choice has no effect on the evaluation of the expression? It's very odd to require something to be specified within an operand when it has no impact on the result. Notably, typeof does not suffer from this limitation.

It's not just about code that better expresses itself. Once some arbitrary type argument has been chosen in a nameof expression, such as object?, changing a constraint on a type parameter can break uses of nameof unnecessarily. Insult becomes added to injury in this scenario. Satisfying the type parameter requires declaring a dummy class to implement an interface which is constraining the type parameter. Now there's unused metadata and a strange name invented, all for the purpose of adding a type argument to the nameof expression, a type argument which `na

@jnm2
jnm2 / Collection interfaces.md
Last active October 2, 2023 19:27
.NET collection interfaces
flowchart
    ICollection --- IEnumerable
    IList --- ICollection
    IEnumerableT[IEnumerable&lt;T&gt;] --- IEnumerable
    ICollectionT[ICollection&lt;T&gt;] --- IEnumerableT
    IListT[IList&lt;T&gt;] --- ICollectionT
    IReadOnlyCollectionT[IReadOnlyCollection&lt;T&gt;] --- IEnumerableT
    IReadOnlyListT[IReadOnlyList&lt;T&gt;] --- IReadOnlyCollectionT
@jnm2
jnm2 / WeakEventSource2.cs
Last active September 20, 2023 11:34
Replaces a multicast delegate as an event's backing store. Duplicate behavior, except it is thread safe and holds weak references. (Same as WeakEventSource.cs, but uses WeakDelegate instead of ConditionalWeakTable.)
// MIT license, copyright 2015 Joseph N. Musser II
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace jnm2
{
public struct WeakDelegate<TDelegate> : IEquatable<WeakDelegate<TDelegate>> where TDelegate : class
/// <summary>
/// Tracks how many references there are to a disposable object, and disposes it when there are none remaining.
/// </summary>
public sealed class RefCountingDisposer
{
private readonly IDisposable disposable;
private uint refCount = 1;
private readonly object lockObject = new();
/// <summary>
using System;
using System.Threading;
internal sealed class DeferredAction
{
private static readonly Action SentinelNextSetShouldPost = () => { };
private readonly SynchronizationContext synchronizationContext;
private Action scheduledAction = SentinelNextSetShouldPost;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
public static class TaskTupleExtensions
{
#region (Task<T1>)
public static TaskAwaiter<T1> GetAwaiter<T1>(this ValueTuple<Task<T1>> tasks)
{