Skip to content

Instantly share code, notes, and snippets.

@sgaliamov
Last active May 2, 2021 15:16
Show Gist options
  • Select an option

  • Save sgaliamov/bf2d391c2aad0d4f31dd29b966a992b0 to your computer and use it in GitHub Desktop.

Select an option

Save sgaliamov/bf2d391c2aad0d4f31dd29b966a992b0 to your computer and use it in GitHub Desktop.
var method = new DynamicMethod("Foo", typeof(int), new[] { typeof(int) });
var generator = method.GetILGenerator();
var label = generator.DefineLabel();
var il = generator.UseIlluminator(); // Creates wrapper
il.Ldarg_0()
.Ldc_I4_2()
.Ceq()
.Brfalse_S(label) // if (value == 2)
.Ldc_I4_1()
.Ret() // return 1
.MarkLabel(label)
.Ldarg_0()
.Ldc_I4_3()
.Add()
.Ret(); // return value + 3
var foo = method.CreateDelegate<Func<int, int>>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment