Skip to content

Instantly share code, notes, and snippets.

View RichardD2's full-sized avatar

Richard Deeming RichardD2

View GitHub Profile
@RichardD2
RichardD2 / Rss10FeedFormatter.cs
Last active August 29, 2015 14:11
An RSS feed formatter which supports parsing v0.91 format feeds.
/*
* An RSS feed formatter which supports parsing v0.91 format feeds.
*
* Based on:
* http://referencesource.microsoft.com/#System.ServiceModel/System/ServiceModel/Syndication/Rss20FeedFormatter.cs
*/
using System;
using System.Collections.Generic;
using System.Globalization;
@RichardD2
RichardD2 / gist:ef4ee339ce3dcc10264b
Last active August 29, 2015 14:13
WPF Aero Glass Effect
internal static class SafeNativeMethods
{
[DllImport("dwmapi", SetLastError = true)]
private static extern int DwmIsCompositionEnabled([MarshalAs(UnmanagedType.Bool)] out bool isEnabled);
public static bool SafeDwmIsCompositionEnabled()
{
bool result = false;
if (Environment.OSVersion.Version.Major >= 6)
{
@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 / ComparerExtensions.cs
Created June 17, 2015 15:17
Extension methods for working with IComparer<T> classes.
using System;
using System.Collections.Generic;
namespace NBS.Core.Collections.Generic
{
public static class ComparerExtensions
{
private sealed class ReverseComparer<T> : IComparer<T>
{
public readonly IComparer<T> Original;
@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 / 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 / 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;
@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 / 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 / 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