Skip to content

Instantly share code, notes, and snippets.

View chrcar01's full-sized avatar

Chris Carter chrcar01

View GitHub Profile
@chrcar01
chrcar01 / keybase.md
Created March 14, 2019 15:34
keybase proof

Keybase proof

I hereby claim:

  • I am chrcar01 on github.
  • I am chriscarter3 (https://keybase.io/chriscarter3) on keybase.
  • I have a public key ASCH8bbvwZrHAs5CC3Se2g_LFIsApYodRUGKUIBJnXgdpQo

To claim this, I am signing this object:

@chrcar01
chrcar01 / keybase.md
Created November 17, 2019 01:56
Keybase proof

Keybase proof

I hereby claim:

  • I am chrisjcarter on github.
  • I am chrisjcarter (https://keybase.io/chrisjcarter) on keybase.
  • I have a public key ASDmpdvoCSUVgVy3WdlhQeWTlqcp_y-UFW5ZWMxlGqyZBQo

To claim this, I am signing this object:

@chrcar01
chrcar01 / keybase.md
Created November 17, 2019 02:04
Keybase proof

Keybase proof

I hereby claim:

  • I am chrcar01 on github.
  • I am chrisjcarter (https://keybase.io/chrisjcarter) on keybase.
  • I have a public key ASDmpdvoCSUVgVy3WdlhQeWTlqcp_y-UFW5ZWMxlGqyZBQo

To claim this, I am signing this object:

@chrcar01
chrcar01 / LazyConstructor.cs
Last active November 26, 2019 04:56
When legacy code is untestable and concrete types have 30+ constructor parameters...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace LazyCreatorCon
{
/// <summary>
/// Creates an instance of a shitty class that can't be mocked,
/// can't be changed, and requires dozens of constructors.
@chrcar01
chrcar01 / GenericComparer.cs
Last active March 31, 2021 14:56
GenericComparer<T>
public class GenericComparer<T> : IComparer<T>
{
private readonly Func<T, IComparable> _comparer;
private GenericComparer(Func<T, IComparable> comparer)
{
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
}
public static GenericComparer<T> Create(Func<T, IComparable> comparer)
@chrcar01
chrcar01 / ProjectSourcePath.cs
Last active March 19, 2022 20:56
Code to find project root path in a visual studio project
using System.Runtime.CompilerServices;
// Ripped from S.O. answer: https://stackoverflow.com/a/66285728/1594171
// 1. Put this code in ProjectSourcePath.cs at root of project
// 2. Reference project path like this: ProjectSourcePath.Value
internal static class ProjectSourcePath
{
private const string myRelativePath = nameof(ProjectSourcePath) + ".cs";
private static string? lazyValue;
public static string Value => lazyValue ??= CalculatePath();
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
// https://www.pinvoke.net/default.aspx/shlwapi.assocquerystring
// https://docs.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-assocquerystringa
// https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-findexecutablea
// https://stackoverflow.com/a/9540278
/// <summary>
/// Helper for working with calendar concepts.
/// </summary>
public static class CalendarHelper
{
/// <summary>
/// Returns all of the weeks for specified calendar month.
/// </summary>
public static IEnumerable<Week> GetWeeksForCalendarMonth(DateOnly month)
{
@chrcar01
chrcar01 / AsyncFileStreamMinApi.cs
Last active January 20, 2023 20:41
Async FileStreams: Example of how to produce correct OpenAPI docs for returning files as streams and byte arrays.
async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(s => s.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Report API",
Description = "Manager all of your reporting needs.",
Version = "v1",
TermsOfService = null,
/*
* Arguments class: application arguments interpreter
*
* Authors: R. LOPES
* Contributors: R. LOPES
* Created: 25 October 2002
* Modified: 28 October 2002
*
* Version: 1.0
*/