Skip to content

Instantly share code, notes, and snippets.

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)
View StartupHook.cs
public static void Initialize()
{
var loadContext = new StartupAssemblyLoadContext();
var assembly = loadContext.LoadFromAssemblyName(new AssemblyName("GCCollector"));
assembly.CreateInstance("GCCollector.Poller");
}
View StartupHook.cs
internal class StartupHook
{
public static void Initialize()
{
new Thread(PollGCMetrics)
{
IsBackground = true,
Name = "GCMetricsPoller"
}.Start();
}
View SingleThreadedSynchronizationContext.cs
public class SingleThreadedSynchronizationContext : SynchronizationContext
{
private Thread _thread;
private BlockingCollection<Action> _queue = new BlockingCollection<Action>();
public SingleThreadedSynchronizationContext()
{
_thread = new Thread(() =>
{