Convert IL code to C# at runtime

邮差的信 提交于 2020-02-03 02:26:07

问题


Is there any free library that allows to transform IL code to C# at runtime?

Thanks, Christian

EDIT

Here is an example of what I have:

In my program, at some point I have a list of strings which resemble the IL code of a class that lies in some assembly:

// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Probant.Arithmetics
       extends [mscorlib]System.Object
{
  .method public hidebysig instance int32 
          Sum(int32 a,
              int32 b) cil managed
  {
    // Code size       11 (0xb)
    .maxstack  2
    .locals init ([0] int32 result,
             [1] int32 CS$1$0000)
    IL_0000:  nop
    IL_0001:  ldarg.1
    IL_0002:  ldarg.2
    IL_0003:  add
    IL_0004:  stloc.0
    IL_0005:  ldloc.0
    IL_0006:  stloc.1
    IL_0007:  br.s       IL_0009

    IL_0009:  ldloc.1
    IL_000a:  ret

Now having this array of strings (each string a line), I would like to somehow generate corresponding C# code.

Does anyone know whether this is possible with ILSpy?


回答1:


ILSpy reads the input assemblies using Mono.Cecil and then works with the Cecils intermediate representation.

If you have the IL as a text rather then binary you would have to find something that can assemble the text into assembly or at least into the Cecils representation.




回答2:


As per me:

public int Sum(int a, int b)
{
    int result,CS$1$0000;
    result = a + b;
    CS$1$0000 = result;

    return CS$1$0000;
}


来源:https://stackoverflow.com/questions/6639884/convert-il-code-to-c-sharp-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!