开发者

Convert IL code to C# at runtime

开发者 https://www.devze.com 2023-03-18 23:27 出处:网络
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:

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 poss开发者_如何学Goible with ILSpy?


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消