This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
internal class InstanceReEntrancyGuard : IDisposable { | |
private class Ref<T> where T : struct { public T Value { get; set; } } | |
private static readonly ConditionalWeakTable<Object, Ref<Boolean>> reEntranceEphemerons = new ConditionalWeakTable<Object, Ref<Boolean>>(); | |
private readonly Ref<Boolean> hasEnteredOnce; | |
public InstanceReEntrancyGuard(Object instance, String reEntrantMessage = null) { | |
hasEnteredOnce = reEntranceEphemerons.GetValue(instance, x => new Ref<Boolean>()); | |
if (hasEnteredOnce.Value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly:Out.Out.Out, @[email protected]] | |
namespace Out { | |
[Out, @[email protected]] public struct Out { | |
[Out, @[email protected]] public class OutAttribute : Attribute {} | |
[Out, @[email protected]] public Out @out([Out, @[email protected]] out Out @out) { return @out.@out(out @out); } | |
} | |
} | |
namespace @out { | |
[Out, Out.Out.Out] public class @out { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly:Ref.Ref.Ref, @[email protected]] | |
namespace Ref { | |
[Ref, @[email protected]] public struct Ref { | |
[Ref, @[email protected]] public class RefAttribute : Attribute {} | |
[Ref, @[email protected]] public Ref @ref([Ref, @[email protected]] ref Ref @ref) { return @ref.@ref(ref @ref); } | |
} | |
} | |
namespace @ref { | |
[Ref, Ref.Ref.Ref] public class @ref { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using System.Windows.Input; | |
class AsyncCommand : ICommand { | |
public Boolean CanExecute(Object parameter = null) => !IsRunning; | |
public event EventHandler CanExecuteChanged = delegate { }; | |
public async void Execute(Object parameter = null) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
public static class Struct { | |
public static Boolean ReferenceEquals<T>(ref T a, ref T b) where T : struct => Cache<T>.Func(ref a, ref b); | |
private static class Cache<T> where T : struct { | |
public delegate Boolean ReferenceEqualsDelegate(ref T a, ref T b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Text.RegularExpressions; | |
public static class AutoPropertyExtensions { | |
const String Prefix = "<"; | |
const String Suffix = ">k__BackingField"; | |
private static String GetBackingFieldName(String propertyName) => $"{Prefix}{propertyName}{Suffix}"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TypeSwitch : ReadOnlyTypeSwitch { | |
public TypeSwitch Case<T>(Action<T> action) { | |
matches.Add(typeof(T), x => action((T) x)); | |
return this; | |
} | |
public ReadOnlyTypeSwitch Default(Action @default) { | |
this.@default = @default ?? delegate { }; | |
return this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
struct LightMutex { | |
private volatile Int32 inUse; | |
public void Lock() { | |
var spinWait = new SpinWait(); | |
while (Interlocked.Exchange(ref inUse, 1) == 1) | |
spinWait.SpinOnce(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private ImmutableArray<THandler> handlers = ImmutableArray<THandler>.Empty; | |
internal static void Add<THandler>(ref ImmutableArray<Action<THandler>> handlers, Action<THandler> handler) { | |
if (handler == null) | |
return; | |
ImmutableArray<Action<THandler>> initial, computed; | |
do { | |
initial = InterlockedGet(ref handlers); | |
computed = initial.Add(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ISoftDeletable { | |
DateTime? Deleted { get; set; } | |
} | |
class MyEntity : ISoftDeletable { | |
public Int64 Id { get; set; } | |
public String Name { get; set; } | |
public DateTime? Deleted { get; set; } | |
} |
OlderNewer