Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- So - After looking at the byte code emitted, it *does* work when compiling with optimizations (more importantly, removing useless code which is used by the debugger). After comparing the bytes emitted against [the CIL instruction list](https://en.wikipedia.org/wiki/List_of_CIL_instructions)
- **In debug mode**
- 0x00 0x00 NOP
- 0x01 0x20 ldc.i4 <int32> Push num of type int32 onto the stack as int32
- 0x02 0xAD argument for ldc.i4
- 0x03 0xDE argument for ldc.i4
- 0x04 0x00 argument for ldc.i4
- 0x05 0x00 argument for ldc.i4
- 0x06 0x0A stloc.0 Pop a value from stack into local variable 0
- 0x07 0x2B br.s <int8> Branch to target, short form.
- 0x08 0x00 argument for br.s
- 0x09 0x06 ldloc.0 Load local variable 0 onto stack
- 0x0A 0x2A ret
- Note instruction `0x2B` - which is jumping back to `0x00`. Decompilation of the same method via LINQPad
- IL_0000: nop
- IL_0001: ldc.i4 AD DE 00 00
- IL_0006: stloc.0 // CS$1$0000
- IL_0007: br.s IL_0009
- IL_0009: ldloc.0 // CS$1$0000
- IL_000A: ret
- Here it's branching to `0009`, which is fine (the branch does nothing, essentially).
- **In release mode**
- 0x20 ldc.i4
- 0xAD argument for ldc.i4
- 0xDE argument for ldc.i4
- 0x00 argument for ldc.i4
- 0x00 argument for ldc.i4
- 0x2A ret
- vs LINQPad decompilation:
- IL_0000: ldc.i4 AD DE 00 00
- IL_0005: ret
- The code *does* work in this case, with a slight change to the end of your method:
- var obj = Activator.CreateInstance(type);
- return (R)type.GetMethod(info.Name).Invoke(obj, new object[] { t });
- instead of
- Func<T, R> method = type.GetMethod(info.Name).CreateDelegate(typeof(Func<T, R>)) as Func<T, R>;
- return method(t);
- Whether this invalid branch target is because it's a debugger helper, or whether branch targets are never returned in `GetILAsByteArray`, I'm not sure. At the very least, trying to re-compile a method compiled in `DEBUG` is probably always going to result in weirdness.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement