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 / 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 / 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 / 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 / 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 / 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;