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

How to dynamically create a class?

Yes, you can use System.Reflection.Emit namespace for this. It is not straight forward if you have no experience with it, but it is certainly possible. Edit: This code might be flawed, but it will give you the general idea and hopefully off to a good start towards the goal. using System; using System.Reflection; using System.Reflection.Emit; … Read more

tech