Skip to content

Instantly share code, notes, and snippets.

View j2jensen's full-sized avatar

James Jensen j2jensen

  • Salt Lake City, UT
View GitHub Profile
@j2jensen
j2jensen / WriteAndReadAvroFileUsingReflection.cs
Last active March 8, 2023 12:13
Write avro file to a memory stream, then read it out again, using reflection.
// Write avro file to a memory stream, then read it out again, using reflection.
// Based on the very sparse C# docs from https://avro.apache.org/docs/1.11.1/api/csharp/html/md_src_apache_main_Reflect_README.html
// combined with the Java docs from https://avro.apache.org/docs/1.11.1/getting-started-java/
var baseClassSchema = @"
[
{ ""type"" : ""record"", ""name"" : ""Derived1"", ""fields"" :
[
{ ""name"" : ""A"", ""type"" : ""string""},
{ ""name"" : ""B"", ""type"" : ""int""}
@j2jensen
j2jensen / ExpressionReplacer.cs
Last active September 4, 2018 21:28
Create "OR"ed Expression from two expressions
public class ExpressionReplacer : ExpressionVisitor
{
private readonly Func<Expression, Expression> replacer;
public ExpressionReplacer(Func<Expression, Expression> replacer)
{
this.replacer = replacer;
}
public override Expression Visit(Expression node)
@j2jensen
j2jensen / Program.cs
Last active July 21, 2016 15:35 — forked from jeremDev/Program.cs
Bedtime Routine
using System;
using static System.Console;
using static System.Threading.Thread;
using Throwable = System.Exception;
public class Parent
{
public string Name => "Parent";
@j2jensen
j2jensen / Require.cs
Created April 28, 2014 16:35
Truenorth.Utility.Contract.Require class, for Require.That() syntax
using System;
using JetBrains.Annotations;
namespace Truenorth.Utility.Contract
{
/// <summary>
/// Some utility methods to help us create assertions within our methods.
/// We would have liked to use Microsoft's CodeContracts, but they made them
/// too clunky.
/// </summary>
@j2jensen
j2jensen / gist:1165918
Created August 23, 2011 17:23
Benchmark Template
/* This is a benchmarking template I use in LINQPad when I want to do a
* quick performance test. Just give it a couple of actions to test and
* it will give you a pretty good idea of how long they take compared
* to one another. It's not perfect: You can expect a 3% error margin
* under ideal circumstances. But if you're not going to improve
* performance by more than 3%, you probably don't care anyway.*/
void Main()
{
// Enter setup code here
var actions = new[]
@j2jensen
j2jensen / BenchmarkTemplate.cs
Created November 10, 2010 00:22
Fill in the blanks to test the difference in performance between different actions.
void Main()
{
var actions = new[]
{
new TimedAction("first", () =>
{
// Insert logic here.
}),
new TimedAction("second", () =>
{
@j2jensen
j2jensen / testresults.txt
Created October 25, 2010 19:07
The following unit test causes a null reference exception intermittently.
[TestMethod]
public void AllRepositoriesShouldBeCreatableViaDependencyInjection()
{
// By doing this in parallel, we simultaneously test whether our
// DI stuff can handle lots of multi-threaded requests.
var repositoryTypes = from a in CoreAssemblies.AsParallel()
from t in a.GetTypes()
where t.Name.EndsWith("Repository")
// Don't bother if they've been flagged as factory-built
where !t.GetCustomAttributes(typeof (FactoryBuiltAttribute), false).Any()