0

I am creating a type with reflection by using the code from this answer. And I want to add a function to this class (probably using ILGenerator). And in the comment section of this answer, Sergey Mirvoda commented that ILGenerator emit commands can be obtained by simply following these steps: compile it into assembly and open with reflector, switch to IL view and you will see code that copypastable

I do not want to decompile so that I can get the lost code. Therefore I am not looking for a decompiler. I am trying to get IL Emit codes for a sample code I currently have. Decompiling might just do the trick. However there may be other solutions and hopefully not requiring third party apps.

I am using VS Ultimate 2013. Is there a way to do this with VS? Or do I need to use third party apps?

Community
  • 1
  • 1
Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67

1 Answers1

0

If you want to get the IL opcodes, for example:

ldarg.0
conv.i8
dup
mul
ret

Then you have several options, including ildasm (included in the Windows SDK), dotPeek's IL viewer or LINQPad's IL tab (all of them gratis).

If you want a tool that automatically generates Reflection.Emit code, e.g.:

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Conv_I8);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Ret);

Then the only one I know of is the ReflectionEmitLanguage add-on to Reflector (which is paid).

svick
  • 236,525
  • 50
  • 385
  • 514
  • https://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator(v=vs.110).aspx ILGenerator allows you to do what you show in your second snippet. – user9993 Jun 01 '16 at 15:59
  • @user9993 Yes, I know. My answer is about how to decompile existing code into code that can be used with `ILGenerator`. – svick Jun 01 '16 at 16:23