是否有可用于访问 .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 上执行此操作,是否没有一种简单的方法来访问此功能,例如使用简单的方法调用?是否有其他库更适合这种特定情况?
最佳答案
那么 .NET 框架提供的 ildasm.exe 工具可以在命令行环境中使用它来进行反汇编,它可能是一个很好的起点。
这是 MSDN Documentation在命令行选项上。
关于.net - 以编程方式访问 .NET 类型的 CIL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2774712/