Call and Callvirt

When the runtime executes a call instruction it’s making a call to an exact piece of code (method). There’s no question about where it exists. Once the IL has been JITted, the resulting machine code at the call site is an unconditional jmp instruction. By contrast, the callvirt instruction is used to call virtual methods … Read more

Fast creation of objects instead of Activator.CreateInstance(type)

I did some benchmarking between these (I would write down the bare minimum details): public static T Instance() //~1800 ms { return new T(); } public static T Instance() //~1800 ms { return new Activator.CreateInstance<T>(); } public static readonly Func<T> Instance = () => new T(); //~1800 ms public static readonly Func<T> Instance = () … Read more

General purpose FromEvent method

Here you go: internal class TaskCompletionSourceHolder { private readonly TaskCompletionSource<object[]> m_tcs; internal object Target { get; set; } internal EventInfo EventInfo { get; set; } internal Delegate Delegate { get; set; } internal TaskCompletionSourceHolder(TaskCompletionSource<object[]> tsc) { m_tcs = tsc; } private void SetResult(params object[] args) { // this method will be called from emitted IL … Read more