Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
thomaslevesque / Strings.tt
Last active February 5, 2021 15:06
Generates a strongly typed wrapper for a resource file, compatible with the Portable Class Library. Just drop this T4 template in your project, with the same base name as a .resx file in the same folder (e.g. Strings.tt for Strings.resx)
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Collections.Generic" #>
@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 / AsyncStackTrace.cs
Created August 16, 2013 13:42
Extension method to add "logical" stack trace to async methods, with usage example. Copy/paste in Linqpad and add the following namespaces: - System.Runtime.CompilerServices - System.Threading.Tasks
async void Main()
{
try
{
await FooAsync().AsyncTrace();
}
catch(Exception ex)
{
ex.FullTrace().Dump();
}
// Solution for James Michael Hare's anagram challenge (http://geekswithblogs.net/BlackRabbitCoder/archive/2015/07/28/little-puzzlersndashlist-all-anagrams-for-a-word.aspx)
// Run in LinqPad, adding a reference to Microsoft.Experimental.Collections
void Main()
{
string path = @"D:\tmp\words.txt";
var dict = new MultiValueDictionary<string, string>();
foreach (var word in File.ReadLines(path))
{
dict.Add(new string(word.OrderBy(c => c).ToArray()), word);
}
@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 / FullOuterJoinTests
Last active July 20, 2017 20:24
AssertThrowsWhenArgumentNull: helper to test correct validation of null arguments
using System;
using NUnit.Framework;
namespace MyLibrary.Tests.XEnumerableTests
{
[TestFixture]
class FullOuterJoinTests
{
[Test]
public void FullOuterJoin_Throws_If_Argument_Null()
@thomaslevesque
thomaslevesque / ThemeInfo.cs
Created September 6, 2012 08:33
Get information about current Windows theme in C#
public class ThemeInfo
{
private readonly string _themeName;
private readonly string _themeColor;
private readonly string _themeSize;
private readonly string _themeFileName;
public ThemeInfo(string name, string fileName, string color, string size)
{
_themeName = name;

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