Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Gosse kevingosse

View GitHub Profile
@kevingosse
kevingosse / Program.cs
Created Mar 26, 2022
Extract native code from dynamic method
View Program.cs
using Microsoft.Diagnostics.Runtime;
using System;
using System.Linq;
namespace ExtractDynamicMethod
{
internal class Program
{
static void Main(string[] args)
{
View Benchmark.cs
[MemoryDiagnoser]
public class EmptyEnumerator
{
[Benchmark(Baseline = true)] public bool ArrayEmpty() => Array.Empty<string>().GetEnumerator().MoveNext();
[Benchmark] public bool ArrayEmptyAsEnumerable() => ((IEnumerable<string>)Array.Empty<string>()).GetEnumerator().MoveNext();
[Benchmark] public bool ArrayEmptyForEach()
{
foreach (var _ in Array.Empty<string>())
View ClrmdAsyncLocal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Diagnostics.Runtime;
namespace ClrmdAsyncLocal
{
internal class Program
{
View IsEvenSourceGenerator.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace IsEvenSourceGenerator
{
[Generator]
View Program.cs
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace BenchmarkCore
{
class Program
{
static void Main(string[] args)
{
View ApiWebRequestFactoryTests.cs
/// <summary>
/// This test ensures that the ApiWebRequestFactory behaves correctly when
/// a different type of WebRequest is assigned to the http:// prefix
/// </summary>
[Fact]
public void OverrideHttpPrefix()
{
// Couldn't find a way to "officially" unregister a prefix but that shouldn't stop us
var prefixListProperty = typeof(WebRequest).GetProperty("PrefixList", BindingFlags.Static | BindingFlags.NonPublic);
var oldPrefixList = prefixListProperty.GetValue(null);
View ApiWebRequestFactory.cs
internal class ApiWebRequestFactory : IApiRequestFactory
{
public IApiRequest Create(Uri endpoint)
{
return new ApiWebRequest(WebRequest.CreateHttp(endpoint));
}
}
View Program.cs
WebRequest.RegisterPrefix("http://", new CustomWebRequestFactory());
View ApiWebRequestFactory.cs
internal class ApiWebRequestFactory : IApiRequestFactory
{
public IApiRequest Create(Uri endpoint)
{
return new ApiWebRequest((HttpWebRequest)WebRequest.Create(endpoint));
}
}
View StartupAssemblyLoadContext.cs
class StartupAssemblyLoadContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver _resolver;
public StartupAssemblyLoadContext()
{
_resolver = new AssemblyDependencyResolver(Assembly.GetExecutingAssembly().Location);
}
protected override Assembly Load(AssemblyName assemblyName)