Skip to content

Instantly share code, notes, and snippets.

View agocke's full-sized avatar

Andy Gocke agocke

View GitHub Profile
static extern void MyPInvoke(IntPtr ptrToCstr)
static void Main()
{
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
try
{
MyPInvoke(ptr);
IntPtr cstrPtr = Marshal.PtrToStructure(ptr, typeof(IntPtr))
20000100000
20000100000
20000100000
20000100000
20000100000
20000100000
; Assembly listing for method Name.Program:IfeHelper(struct):long:this
; Emitting BLENDED_CODE for X64 CPU with AVX
; optimized code
; rbp based frame
class C
{
[DllImport...]
private static extern IntPtr GetAHandle();
private static Task WrapHandle()
{
var handle = GetAHandle();
var tcs = new TaskCompletionSource();
Task.Run(() =>
@agocke
agocke / tests.cs
Created September 7, 2017 19:28
Interesting output from local functions
public class C {
public void M() {
int x = 0;
{
int y = 0;
// Captures two struct closures
int L() => x + y;
}
}
@agocke
agocke / LoginResourceFull.cs
Created June 14, 2018 22:38
C# data class example
using System;
public class LoginResource : IEquatable<LoginResource>
{
public string Username { get; set; }
public string Password { get; set; }
public bool RememberMe { get; set; } = false;
public override bool Equals(object obj)
=> obj is LoginResource resource && Equals(resource);
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace misc_bench
{
[MemoryDiagnoser]
public class Benchmarks
@agocke
agocke / DefaultImplPrinter.cs
Created May 21, 2021 05:26
Prints signatures of implementations of interfaces in interfaces
using System;
using System.Collections.Immutable;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
namespace default_impl
{
@agocke
agocke / IsRef.cs
Created May 27, 2021 01:29
Check if assembly is a ref assembly
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
try
{
using var file = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var pe = new PEReader(file);
ref struct S<#a> {
public int Field;
public ref<#a> int RefField;
}
static int StaticField = 5;
public void M1() {
S s = default; // S<#global>
s = new S() { RefField = ref StaticField };
interface IWebApplication<abstract TResult>
{
TResult MapGet(string route, Func<TResult, TResult> f);
}
class NotFound : IResult { }
class OK<TValue> : IResult { }
class MyWebApp : IWebApplication<IResult>
{