Skip to content

Instantly share code, notes, and snippets.

View bbarry's full-sized avatar

Bill Barry bbarry

  • Ren Inc
  • Pennsylvania
View GitHub Profile
@bbarry
bbarry / consoleapp.cs
Last active October 14, 2015 17:06
use reflection emit to create a class with a static and instance member that have the same name
//example emit code borrowed and lightly modified from http://www.c-sharpcorner.com/uploadfile/puranindia/reflection-and-reflection-emit-in-C-Sharp/
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
namespace ConsoleApplication12 {
// declare the interface
@bbarry
bbarry / Nullabletestprogram.cs
Created November 1, 2015 00:51
`a is int?` compiles
using System;
using NullableInt = System.Nullable<int>;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("null:");
@bbarry
bbarry / Tuples.cs
Last active November 8, 2015 02:37
playing with tuples
namespace JustATest
{
// public struct InheritingTuple(int A, int B) : (int, int)(A, B);
// same: public struct InheritingTuple(int A, int B) : (int A, int B)(A, B);
// same, better?: public struct InheritingTuple(int A, int B) : (int A: A, int B: B);
// compiles as:
// public struct InheritingTuple(int A, int B) : ValueTuple<int, int>(A, B);
// should this be allowed?
// I think no: ValueTuple<T...> structs sealed?
// where would attribute go?
using System;
using System.Reflection;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
@bbarry
bbarry / Program.cs
Last active November 23, 2015 20:19
This console app simulates various ways string interpolation in C# could be implemented. By manipulating the parameters array in the template file one can produce different versions of a particular format string. By hitting different methods internally via the number and arrangement of parameters to the template one can produce a variety of perf…
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
namespace ConsoleApplication1 {
internal partial class Program {
private const int Iterations = 10000;
private static void Main() {
[Fact]
public void NewArrayUsesCachedArray() {
var verifier = CompileAndVerify(@"
namespace System
{
public class Object { }
public class ValueType { }
public struct Int32 { }
public class String { }
(int, int) LocateRange<TKey, TValue> (SortedList<TKey, TValue> list, Func<Tkey, bool> beforeRange, Func<Tkey, bool> inRange, Func<Tkey, bool> afterRange)
{
//
// searches list for the range of items that meet the specified criteria
// returns (-1, -1) if nothing found or the indices if it is
//
// example searching a string keyed list by items that start with some value:
//
// given:
@bbarry
bbarry / SliceExtensions.cs
Last active December 1, 2015 15:13 — forked from fubar-coder/SliceExtensions.cs
Simple slice implementation for C#
public static class SliceExtensions
{
public static Slice<T> ToSlice<T>(this T[] array)
{
return new Slice<T>(array);
}
public static Slice<T> ToSlice<T>(this T[] array, int offset, int length)
{
return new Slice<T>(array, offset, length);
{
// Code size 66 (0x42)
.maxstack 7
IL_0000: ldnull
IL_0001: ldtoken ""int Test.ModAdd2(params int[])""
IL_0006: call ""System.Reflection.MethodBase System.Reflection.MethodBase.GetMethodFromHandle(System.RuntimeMethodHandle)""
IL_000b: castclass ""System.Reflection.MethodInfo""
IL_0010: ldc.i4.1
IL_0011: newarr ""System.Linq.Expressions.Expression""
@bbarry
bbarry / DataStoreCodeGen.cs
Created December 5, 2015 00:10
Builds a strongly typed data storage thing via Reflection.Emit.
// the meat and potatoes...
public class DataStoreCodeGen {
private const TypeAttributes public_sealed_class = TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit;
private const MethodAttributes public_ctor = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
private const MethodAttributes get_set = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.SpecialName;
// singleton reference to module containing all generated storage interfaces
private static ModuleBuilder ModuleBuilder { get; } = CreateModuleBuilder();