以编程方式访问 .NET 类型的 CIL

发布于 2024-08-31 02:40:35 字数 1410 浏览 5 评论 0原文

是否有一个直接的库可用于访问 .NET 类型的 CIL?让我演示一下我想要虚构的 CilExtractor 执行的操作:

[Serializable]
public class Type_For_Extract_Cil_Test {

  private int _field = 3;

  public int Method(int value) {
    checked {
      return _field + value;
    }
  }

}

[Test]
public void Extract_Cil_For_Type_Test() {
  string actualCil = CilExtractor.ExtractCil(typeof(Type_For_Extract_Cil_Test));
  string expectedCil = @"
    .class public auto ansi serializable beforefieldinit Type_For_Extract_Cil_Test
      extends [mscorlib]System.Object
    {
      .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
      {
        .maxstack 8
        ldarg.0 
        ldc.i4.3 
        stfld int32 Type_For_Extract_Cil_Test::_field
        ldarg.0 
        call instance void [mscorlib]System.Object::.ctor()
        ret 
      }

      .method public hidebysig instance int32 Method(int32 'value') cil managed
      {
        .maxstack 8
        ldarg.0 
        ldfld int32 Type_For_Extract_Cil_Test::_field
        ldarg.1 
        add.ovf 
        ret 
      }

      .field private int32 _field
    }";
  // indentations and code formatting issues apart, this should succeed
  Assert.AreEqual(expectedCil, actualCil);
}

我知道我可以使用 Mono.Cecil 或 Reflector 来完成此操作,但我也知道我必须编写大量代码才能实现此目的。由于 Reflector 已经在其 UI 上执行了此操作,因此是否有一种简单的方法来访问此功能,例如通过简单的方法调用?还有其他库更适合这种特定场景吗?

Is there a straighforward library that I can use to access the CIL for a .NET type? Let me demonstrate what I want the fictitious CilExtractor to do:

[Serializable]
public class Type_For_Extract_Cil_Test {

  private int _field = 3;

  public int Method(int value) {
    checked {
      return _field + value;
    }
  }

}

[Test]
public void Extract_Cil_For_Type_Test() {
  string actualCil = CilExtractor.ExtractCil(typeof(Type_For_Extract_Cil_Test));
  string expectedCil = @"
    .class public auto ansi serializable beforefieldinit Type_For_Extract_Cil_Test
      extends [mscorlib]System.Object
    {
      .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
      {
        .maxstack 8
        ldarg.0 
        ldc.i4.3 
        stfld int32 Type_For_Extract_Cil_Test::_field
        ldarg.0 
        call instance void [mscorlib]System.Object::.ctor()
        ret 
      }

      .method public hidebysig instance int32 Method(int32 'value') cil managed
      {
        .maxstack 8
        ldarg.0 
        ldfld int32 Type_For_Extract_Cil_Test::_field
        ldarg.1 
        add.ovf 
        ret 
      }

      .field private int32 _field
    }";
  // indentations and code formatting issues apart, this should succeed
  Assert.AreEqual(expectedCil, actualCil);
}

I know I can do this with Mono.Cecil or Reflector, but I also know I have to write a lot of code to achieve this. Since Reflector already does this on its UI, isn't there a simple way to access this functionality, like with a simple method call? Are there other libraries that are better suited to this specific scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-09-07 02:40:35

.NET 框架提供了 ildasm.exe 工具,可以在命令行环境中使用它来进行反汇编,这可能是一个不错的起点。

以下是该命令的 MSDN 文档 - 行选项。

Well there is the ildasm.exe tool provided by the .NET framework and it can be used in a command-line environment to do the disassembly, it might be a good place to start.

Here is the MSDN Documentation on the command-line options.

浮华 2024-09-07 02:40:35

除了 ildasm 之外,我不知道还有什么工具可以以文本形式提取反汇编代码。

我认为 Mono.Cecil 能够检索有关程序集及其类型的所有元数据,包括CIL 代码。该集合由对象的集合表示,但不是以文本形式。

也许内置的 Reflection.Emit 东西也可以提取 CIL,但我不确定。

I don't know of a tool for extracting the disassembly in text form, other than ildasm.

I think Mono.Cecil is able to retrieve all the metadata about an assembly and its types, including the CIL code. The assembly is represented by a collection of objects, however, not in textual form.

Probably the built-in Reflection.Emit stuff can extract CIL too, but I'm not sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文