Skip to content

Instantly share code, notes, and snippets.

View RichardD2's full-sized avatar

Richard Deeming RichardD2

View GitHub Profile
@RichardD2
RichardD2 / ClipboardHelper.cs
Created April 25, 2022 07:42
Windows 10 Clipboard Utilities
using System;
using System.Windows;
public static class ClipboardHelper
{
public static void SetString(string value)
{
if (!DataTransferHelper.TrySetString(value))
{
Clipboard.SetDataObject(value);
@RichardD2
RichardD2 / IQueryableExtensions.cs
Last active September 14, 2021 15:35 — forked from ErikEJ/IQueryableExtensions.cs
Replacement for EF Core .Contains, that avoids SQL Server plan cache pollution
public static class IQueryableExtensions
{
private readonly struct HalfList<T>
{
private readonly IReadOnlyList<T> _list;
private readonly int _startIndex;
private HalfList(IReadOnlyList<T> list, int startIndex, int count)
{
@RichardD2
RichardD2 / TimestampExtensions.cs
Created February 5, 2021 08:46
UrlHelper extension to append a timestamp to a CSS / JS URL
using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
using System.Web.Mvc;
namespace NBS.Core.Web.Mvc
{
@RichardD2
RichardD2 / SqlQueryFormatter.cs
Last active March 27, 2020 21:17
SQL Query Formatter
using System;
using System.Data;
using System.IO;
public static class SqlQueryFormatter
{
public static string FormatCommand(this IDbCommand command)
{
if (command is null) throw new ArgumentNullException(nameof(command));
@RichardD2
RichardD2 / XmlDefaultNamespaceExtensions.cs
Created November 8, 2018 11:26
XLinq extension methods to select elements using the default namespace of the container.
using System;
using System.Collections.Generic;
using System.Linq;
namespace System.Xml.Linq
{
public static class XmlDefaultNamespaceExtensions
{
public static IEnumerable<XElement> DescendantsNs(this XElement container, string name)
=> container.Descendants(container.Name.Namespace + name);
public sealed class AsIsBundleOrderer : IBundleOrderer
{
private AsIsBundleOrderer()
{
}
public static IBundleOrderer Instance { get; } = new AsIsBundleOrderer();
public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files) => files;
}
@RichardD2
RichardD2 / HtmlElementAttributeTagHelper.cs
Last active June 27, 2018 17:25
ASP.NET Core TagHelper to load element attributes from ViewData.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace AspNetCore.Helpers
{
[HtmlTargetElement(Attributes = ViewBagKeyAttributeName)]
public class HtmlElementAttributeTagHelper : TagHelper
@RichardD2
RichardD2 / MorrisList.cs
Created December 1, 2017 14:00
Morris Sequence
using System;
using System.Collections.Generic;
namespace Morris
{
public sealed class MorrisList : IEnumerable<byte>
{
private sealed class MorrisNode
{
public const int Size = 80000;
@RichardD2
RichardD2 / SmtpClientExtensions.cs
Last active January 28, 2019 10:35 — forked from mattbenic/SmtpClientExtensions.cs
Extension method to have SmtpClient's SendMailAsync respond to a CancellationToken
using System;
using System.ComponentModel;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
public static class SmtpClientExtensions
{
/// <summary>
/// Sends the specified message to an SMTP server for delivery as an asynchronous operation.
@RichardD2
RichardD2 / AsyncThrottle.cs
Last active November 10, 2016 18:56
Async throttle, inspired by code from Stephen Toub's blog - http://blogs.msdn.com/b/pfxteam/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public sealed class Throttle
{
private readonly Queue<TaskCompletionSource<IDisposable>> _waiters = new Queue<TaskCompletionSource<IDisposable>>();
private readonly WaitCallback _releaseCoreCallback;
private readonly Task<IDisposable> _releaserTask;