Skip to content

Instantly share code, notes, and snippets.

View RichardD2's full-sized avatar

Richard Deeming RichardD2

View GitHub Profile
@RichardD2
RichardD2 / RawPrinterHelper.cs
Created November 1, 2016 13:34
Raw printer helper
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
internal static class RawPrinterHelper
{
[StructLayout(LayoutKind.Sequential)]
@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 / AnonymousComparer.cs
Created June 17, 2015 20:17
Build an IComparer<T> for multiple properties, based on a mapping to an anonymous type.
public static class AnonymousComparer
{
private static class DefaultComparerCache
{
private static readonly ConcurrentDictionary<Type, Expression> Cache = new ConcurrentDictionary<Type, Expression>();
private static Expression GetDefaultComparerCore(Type propertyType)
{
var genericTypeDefinition = typeof(Comparer<>);
var comparerType = genericTypeDefinition.MakeGenericType(propertyType);
@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 / DaylightHours.cs
Last active January 7, 2020 09:49
Sunrise / Sunset calculation
using System;
using System.Diagnostics;
namespace Trinet.Core
{
/// <summary>
/// Represents the time of sunrise and sunset for a date and location.
/// </summary>
/// <remarks>
/// <para>The calculated times have a precision of approximately three mintes.
@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 / 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;
}