Skip to content

Instantly share code, notes, and snippets.

View trinitrotoluene's full-sized avatar
🧊

trinitrotoluene trinitrotoluene

🧊
View GitHub Profile
@trinitrotoluene
trinitrotoluene / ActivatorUtilities.cs
Last active October 22, 2019 12:48
Better Activation with Expression<T>
internal delegate T InstanceCreator<T>(params object[] ctorArgs);
internal delegate object InstanceCreator(params object[] ctorArgs);
internal static class ActivatorUtilities
{
private static readonly ConcurrentDictionary<Type, Delegate> TypedActivatorCache = new ConcurrentDictionary<Type, Delegate>();
public static InstanceCreator<T> GetInstanceCreator<T>(ConstructorInfo ctorInfo)
{

Keybase proof

I hereby claim:

  • I am trinitrotoluene on github.
  • I am trinitrotoluene (https://keybase.io/trinitrotoluene) on keybase.
  • I have a public key ASA0TjsM-_Qx3PBYtC9PUK6-HocQnEpSOekTqJcfBf1Wrgo

To claim this, I am signing this object:

@trinitrotoluene
trinitrotoluene / GenericDelegateInvokerExtensions.cs
Last active April 9, 2019 19:40
I read Jon Skeet's article on speeding up MethodInfo#Invoke() and implemented it as extension methods up to 14 parameters. Wrote it so you don't have to, enjoy!
private static Type[] GetGenericArgumentsForMethod<TTarget, TReturn>(MethodInfo methodInfo)
{
var paramInfoArray = methodInfo.GetParameters();
var typeArray = new Type[paramInfoArray.Length + 2];
for (int i = 1; i <= paramInfoArray.Length; i++)
{
typeArray[i] = paramInfoArray[i - 1].ParameterType;
}
typeArray[0] = typeof(TTarget);
public class AsyncSubscriber<TMessage> : IASyncSubscriber
{
private readonly ulong _id;
private readonly Func<TMessage, Task> _messageDelegate;
internal AsyncSubscriber(ulong id, Func<TMessage, Task> messageDelegate)
{
_id = id;
_messageDelegate = messageDelegate;
internal static class ThreadSafeRandom
{
private static readonly Random _global = new Random();
[ThreadStatic]
private static Random _local;
private static Random ThreadLocalRandom
{
get