0

May be it's dump question because i am new to DynamicMethod so Please guide me.

I have C# method store which is serialized in binary file. Data stored is in this fashion .

Token: 6000196 // Meta Token
Flags: 2
MaxStack: 8
LocalVarsSig: null
Code: 00 02 72 ED 0C 00 70 28 72 00 00 06 73 98 01 00 06 28 81 01 00 06 00 02 72 3F 0D 00 70 28 72 00 00 06 73 9F 01 00 06 28 83 01 00 06 00 2A
TokenPosArray: 3 8 D 12 19 1E 23 28
Exceptions: null

I am generating DynamicMethod from this information using DynamicILInfo. I want to know how can i get human readable c# code that from this IL method codec information ?

[Edit]

Ok So i was able to get DynamicMethod constructed from this information and got IL code using ILVisulizer shown at https://github.com/drewnoakes/il-visualizer

When send IL to monitor i get following instruction codes similar to this

IL_0000: nop        
IL_0001: nop        
IL_0002: ldstr      "SampleString"
IL_0007: call       System.String smethod_4(System.String)/GClass3+GClass6
IL_000c: newobj     Void.ctor(System.String)
IL_0011: stloc.0    
IL_0012: ldloc.0    
IL_0013: callvirt   Boolean get_FirstInstance()
IL_0018: stloc.3    
IL_0019: ldloc.3    
IL_001a: brtrue.s   IL_0033
IL_001c: nop        

Now want to know how can i de-compile this code to c# using programming ?

user81907
  • 25
  • 7
  • 1
    This is called "decompilation" and it is not an easy process, you might be best off by finding an existing decompiler that can handle what you're seeing there. – Lasse V. Karlsen Oct 24 '18 at 12:59
  • You can look at [Mono.Cecil](https://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/) as an example. – Lasse V. Karlsen Oct 24 '18 at 12:59
  • I found Mono.Cecil link on lot website . But I still dont know which function to use as starting ? Any example ? – user81907 Oct 24 '18 at 13:13
  • Use ilspy code or telerik decompiler engine code available at github – Access Denied Oct 25 '18 at 13:39
  • You can use AssemblyBuilder.Save() and open that file in a decompiler. You cannot assume this always works well, decompilers were written to expect the kind of IL that a C# compiler generates and may well choke badly on what you did. – Hans Passant Oct 25 '18 at 14:04
  • Any class in ilspy which takes input as DynamicMethod and generate decompiled c# code as output ? – user81907 Oct 25 '18 at 14:04
  • Ok. It seems I can convert code back using AssemblyBuilder.Save() method as described in https://stackoverflow.com/questions/6349332/saving-a-dynamicmethod-to-disk . Question is i am still unclear about How to use AssemblyBuilder to save DynamicMethod ? – user81907 Oct 26 '18 at 04:45

0 Answers0