Skip to content

Instantly share code, notes, and snippets.

using System.Diagnostics;
using System.Net;
using APITesting.Contracts;
namespace YOUR_NAMESPACE_HERE;
public readonly struct ApiError : IEquatable<ApiError>
{
private const string DefaultErrorMessage = "An error occurred";
internal ApiError(HttpStatusCode statusCode, object? data)
@binarycow
binarycow / FontAwesomeExtension.cs
Created January 26, 2024 15:14
Markup extension to create SvgAwesome
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using FontAwesome6;
using FontAwesome6.Svg;
using Microsoft.Extensions.DependencyInjection;
namespace MyNamespace;
[MarkupExtensionReturnType(typeof(SvgAwesome))]
@binarycow
binarycow / ToolBar.xaml
Created January 20, 2024 18:13
ToolBar
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ToolBarDarkFill" Color="#FFEEF5FD" />
<SolidColorBrush x:Key="ToolBarDisabledFill" Color="#FFDADADA" />
<SolidColorBrush x:Key="ToolBarDisabledBorder" Color="#FFDADADA" />
<SolidColorBrush x:Key="ToolBarSeparatorFill" Color="#FFB6BDC5" />
<SolidColorBrush x:Key="ToolBarButtonHover" Color="#210080FF" />
<SolidColorBrush x:Key="ToolBarButtonHoverBorder" Color="#80DADADA" />
<SolidColorBrush x:Key="ToolBarButtonChecked" Color="#400080FF" />
<SolidColorBrush x:Key="ToolBarButtonPressed" Color="#400080FF" />
@binarycow
binarycow / Casting.cs
Created January 21, 2023 20:22
Generic Casting
using System.Collections;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
namespace Test;
public static class Casting
{
private static readonly ConcurrentDictionary<Type, Delegate> cache = new();
@binarycow
binarycow / ConsoleAsync.cs
Created January 8, 2023 23:50
Async console
using System.Text;
using System.Threading.Channels;
namespace Demo;
public interface IConsoleAsync
{
public TimeSpan CharacterDelay { get; set; }
public Task<string?> ReadLineAsync(CancellationToken cancellationToken);
public Task<string?> ReadLineAsync();
@binarycow
binarycow / alpha.yang
Last active December 15, 2022 22:12
yang sample
module alpha {
yang-version 1.1;
prefix a;
namespace urn:alpha;
import bravo {
prefix b;
}
augment /network-device/interface {
@binarycow
binarycow / SimpleTests.cs
Created December 7, 2022 18:42
Enumerable Tests - Solutions
public class SimpleTests : TestBase
{
[Test]
[TestCase(
new []{ 96, 45, 69, 18, 94, 50, 21, 75, 64, 77, 30, 45, 89, 80, 57, 55, 93, 87, 83, 79, 50, 33, 12 },
new [] { 96, 69, 94, 50, 75, 64, 77, 89, 80, 57, 55, 93, 87, 83, 79, 50 }
)]
[TestCase(
@binarycow
binarycow / Error.cs
Created November 18, 2022 23:30
Result
public readonly struct Error : IEquatable<Error>
{
public const string DefaultErrorMessage = "An unspecified error has occurred";
public static Error Default => default;
private readonly string? message;
public string Message => message ?? Exception?.Message ?? DefaultErrorMessage;
public Exception? Exception { get; }
@binarycow
binarycow / DataTemplateResourceDictionary.cs
Last active November 18, 2022 21:16
DataTemplateResourceDictionary.cs
[AttributeUsage(AttributeTargets.Class)]
public sealed class ViewAttribute : Attribute
{
public Type ViewType { get; }
public ViewAttribute(Type viewType) => this.ViewType = viewType;
}
public class DataTemplateResourceDictionary : ResourceDictionary
public static class NumberExtensions
{
public static bool TryConvertChecked<TFrom, TTo>(this TFrom value, out TTo result)
where TFrom : INumberBase<TFrom>
where TTo : INumberBase<TTo>
{
try
{
result = TTo.CreateChecked(value);
return true;