1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| using System; using System.Reflection; using System.Reflection.Emit;
DynamicMethod helloMethod = new DynamicMethod("Hello", typeof(string), new[] { typeof(string)}, true);
ILGenerator ilGenerator = helloMethod.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldstr, "Hello ");
ilGenerator.Emit(OpCodes.Ldarg_0);
MethodInfo concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });
ilGenerator.Emit(OpCodes.Call, concatMethod);
ilGenerator.Emit(OpCodes.Ret);
Func<string, string> func = (Func<string, string>)helloMethod.CreateDelegate(typeof(Func<string, string>));
string result = func("World");
|