Skip to content

Instantly share code, notes, and snippets.

// See https://aka.ms/new-console-template for more information
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
BenchmarkRunner.Run<Benchmarks>();
public class Benchmarks
{
@Ilchert
Ilchert / AspnetFilters.cs
Last active October 5, 2022 09:54
Aspnet minimal api filters
using Microsoft.AspNetCore.TestHost;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseTestServer();
var app = builder.Build();
var group = app.MapGroup("/");
group.AddEndpointFilter(async (efiContext, next) =>
{
var result = await next(efiContext);
@Ilchert
Ilchert / Modules.cs
Last active September 14, 2022 13:16
public static class ServiceCollectionExtensions
{
internal static readonly string ModulesKey = "Modules";
public static IServiceCollection AddModules(this IServiceCollection serviceCollection, HostBuilderContext builderContext)
{
var modulesSection = builderContext.Configuration.GetSection(ModulesKey);
foreach (var moduleName in modulesSection.GetChildren())
{
var type = Type.GetType(moduleName.Value, true, false);
using System.Security.Cryptography;
var time = DateTime.Now;
var files = Directory.EnumerateFiles("C:\\Program Files (x86)\\Steam", "*", SearchOption.AllDirectories)
.AsParallel()
.Select(p => new FileInfo(p))
.GroupBy(p => p.Length)
.Where(p => p.Count() > 1 && p.Key > 0)
.SelectMany(p => p)
class Converter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert
.GetInterfaces()
.Any(p => p.IsGenericType && p.GetGenericTypeDefinition() == typeof(IFloatingPoint<>));
}
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
[JsonConverter(typeof(MyConverter))]
record My(string? String, int? Number);
class MyConverter : JsonConverter<My>
{
public override My? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
async Task Main()
{
var hb = new HostBuilder();
hb
.ConfigureAppConfiguration(p => p.AddInMemoryCollection(new Dictionary<string, string> { { "Property", "Value" } }))
.ConfigureServices(s =>
{
s.AddSingleton(typeof(IConfigureOptions<>), typeof(Confgiuration<>));
});
internal class InstantConverter : IMappingSchemaConverter<Instant, DateTime>
{
private readonly DateTimeZone _currentZone;
public InstantConverter(IConfiguration config, ILogger<InstantConverter> logger)
{
var zoneName = config.GetValue<string>(ViewConfigurationExtensions.DefaultTimezoneConfigurationKey);
var systemDefaultTimeZone = DateTimeZoneProviders.Tzdb.GetSystemDefault();
if (!string.IsNullOrEmpty(zoneName))
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
var data = new A[] { new B { PropA = "A-B", PropB = "B-B" }, new C { PropA = "A-C", PropC = "C-C" } };
var options = new JsonSerializerOptions
private static async IAsyncEnumerable<ReadOnlySequence<byte>> GetJsonAsync(PipeReader reader, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var state = new JsonReaderState(new JsonReaderOptions() { AllowTrailingCommas = true });
try
{
while (true)
{
var rr = await reader.ReadAsync(cancellationToken);
var buffer = rr.Buffer;