Skip to content

Instantly share code, notes, and snippets.

@Rohansi
Created April 8, 2014 05:35
Show Gist options
  • Save Rohansi/10094534 to your computer and use it in GitHub Desktop.
Save Rohansi/10094534 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Reflection.Emit;
namespace ConsoleApplication1
{
class Program
{
public class Context
{
public int A;
}
public delegate void PineMethod(Context context);
static void Main(string[] args)
{
var method = new DynamicMethod("test", typeof(void), new[] { typeof(Context) }, typeof(Program).Module);
var gen = method.GetILGenerator();
var a = typeof(Context).GetField("A");
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Ldfld, a);
gen.Emit(OpCodes.Ldc_I4, 10);
gen.Emit(OpCodes.Add);
gen.Emit(OpCodes.Stfld, a);
var func = (PineMethod)method.CreateDelegate(typeof(PineMethod));
var context = new Context();
context.A = 5;
func(context);
Console.WriteLine("A = {0}", context.A);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment