Created
April 8, 2014 05:35
-
-
Save Rohansi/10094534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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