Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
thomaslevesque / .gitignore
Last active November 3, 2022 07:56
Tests in same project
bin/
obj/
.vs/
.idea/
.vscode/
*.user
@thomaslevesque
thomaslevesque / NaturalSortStringComparer.cs
Created September 14, 2022 01:45
Natural sort string comparer
using System;
using System.Collections.Generic;
/// <summary>
/// String comparer that takes numbers into account.
/// e.g. "Episode 9" will be considered less than "Episode 10", even though it would be greater based on strictly
/// alphabetical order.
/// </summary>
public class NaturalSortStringComparer : IComparer<string>
{
@thomaslevesque
thomaslevesque / TestNullable.cs
Created June 5, 2020 12:01
Repro for CS8762 false positive
using System.Diagnostics.CodeAnalysis;
namespace TestNullable
{
class Test
{
static bool TrySomething1(string s, [NotNullWhen(false)] out string? failReason)
{
// warning CS8762: Parameter 'failReason' must have a non-null value when exiting with 'false'.
return s.Length % 2 == 0
@thomaslevesque
thomaslevesque / gist:274766d874ebf1769c48a203786725f3
Created July 24, 2018 10:05
Regex to fix SwaggerResponse attributes after migration to Swashbuckle 3.0.0
Pattern: \[SwaggerResponse\((?<code>\d+), (?<type>typeof\([A-Za-z\.\<\>]+\)), (?<desc>\"[^\"]+\")\)\]
Replacement: [SwaggerResponse(${code}, ${desc}, ${type})]
@thomaslevesque
thomaslevesque / additional.css
Created August 26, 2017 14:20
Additional CSS for wide-screen Twenty Fourteen Wordpress theme
pre {
padding: 0;
border: none;
}
article h3 {
font-size: 20px;
}
code {
@thomaslevesque
thomaslevesque / CSharpErrorsAndWarnings.md
Last active October 16, 2023 09:59
All C# errors and warnings. NOTE: this list is out of date. See https://github.com/thomaslevesque/GenerateCSharpErrors/blob/master/CSharpErrorsAndWarnings.md for a more recent version

All C# errors and warnings

Parsed from the Roslyn source code using Roslyn.

Code Severity Message
CS0006 Error Metadata file '{0}' could not be found
CS0009 Fatal Metadata file '{0}' could not be opened -- {1}
CS0012 Error The type '{0}' is defined in an assembly that is not referenced. You must add a reference to assembly '{1}'.
CS0016 Error Could not write to output file '{0}' -- '{1}'
@thomaslevesque
thomaslevesque / CSharpErrorCodes.cs
Created May 10, 2017 01:10
Generate "warnings as errors" ruleset from error code definitions in Roslyn source code
void Main()
{
string errorCodesFileUrl = "https://raw.githubusercontent.com/dotnet/roslyn/master/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs";
string errorCodesFileContent = new WebClient().DownloadString(errorCodesFileUrl);
var syntaxTree = CSharpSyntaxTree.ParseText(errorCodesFileContent);
var root = syntaxTree.GetRoot();
var enumDeclaration =
root.DescendantNodes()
.OfType<EnumDeclarationSyntax>()
.First(e => e.Identifier.ValueText == "ErrorCode");
@thomaslevesque
thomaslevesque / ParseAuthChallenge.cs
Created February 23, 2017 00:56
Parsing HTTP authentication challenge with Sprache
void Main()
{
ParseAndPrintChallenge(@"Bearer realm=""FooCorp"", error=invalid_token, error_description=""The access token has expired""");
}
void ParseAndPrintChallenge(string input)
{
var challenge = Grammar.Challenge.Parse(input);
Console.WriteLine($"Scheme: {challenge.Scheme}");
Console.WriteLine($"Parameters:");

Keybase proof

I hereby claim:

  • I am thomaslevesque on github.
  • I am thomaslevesque (https://keybase.io/thomaslevesque) on keybase.
  • I have a public key whose fingerprint is 32C8 E72A EC90 6E8B 79BF 36C3 7AF5 E7A4 FE44 396B

To claim this, I am signing this object:

@thomaslevesque
thomaslevesque / FakeConfigurator.cs
Created June 9, 2016 17:05
Adapter to ease the migration from FakeItEasy 1.x to 2.0.0, to avoid modifying existing fake configurators.
using FakeItEasy;
using FakeItEasy.Creation;
namespace TestUtilies
{
public abstract class FakeConfigurator<T> : FakeOptionsBuilder<T>
{
protected override void BuildOptions(IFakeOptions<T> options)
{
options.ConfigureFake(ConfigureFake);