Look, ma, no *
operator!
using System;
using System.Reflection.Emit;
static class Program
{
delegate uint UintOpDelegate(uint a, uint b);
static void Main()
{
var method = new DynamicMethod("Multiply",
typeof(uint), new Type[] { typeof(uint), typeof(uint) });
var gen = method.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Mul);
gen.Emit(OpCodes.Ret);
var del = (UintOpDelegate)method.CreateDelegate(typeof(UintOpDelegate));
var product = del(2, 3); //product is now 6!
}
}
Even better:
using System;
using System.Runtime.InteropServices;
delegate uint BinaryOp(uint a, uint b);
static class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool VirtualProtect(
IntPtr address, IntPtr size, uint protect, out uint oldProtect);
static void Main()
{
var bytes = IntPtr.Size == sizeof(int) //32-bit? It's slower BTW
? Convert.FromBase64String("i0QkBA+vRCQIww==")
: Convert.FromBase64String("D6/Ki8HD");
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
uint old;
VirtualProtect(handle.AddrOfPinnedObject(),
(IntPtr)bytes.Length, 0x40, out old);
var action = (BinaryOp)Marshal.GetDelegateForFunctionPointer(
handle.AddrOfPinnedObject(), typeof(BinaryOp));
var temp = action(3, 2); //6!
}
finally { handle.Free(); }
}
}