是否可以使用像Reflector这样的反汇编程序来查看成员变量的硬编码值?

时间:2009-10-13 18:42:44

标签: .net reflector disassembly

鉴于下面的示例源代码,有人可以使用反汇编程序查看_secret的值吗?我没有看到通过Reflector获取值的方法,但我没有太多使用它。假设代码没有以任何方式混淆。

class Foo
{
    private string _secret = @"all your base are belong to us";

    public void Foo()
    {
        ...
    }
}

谢谢!

2 个答案:

答案 0 :(得分:4)

它在Reflector的构造函数中可见。

class Foo { private string _secret = @"all your base are belong to us"; }

转换为具有构造函数

public Foo() { this._secret = "all your base are belong to us"; }

在方法Foo中的.ctor下的Reflector中可见。

您还可以在ildasm中的Foo::.ctor : void(Microsoft Visual Studio附带)中查看此信息:

.method public hidebysig specialname rtspecialname instance void .ctor() cil managed {
    // Code size       19 (0x13)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  ldstr      "all your base are belong to us"
    IL_0006:  stfld      string Playground.Foo::_secret
    IL_000b:  ldarg.0
    IL_000c:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0011:  nop
    IL_0012:  ret
} // end of method Foo::.ctor

最后,如果有人知道您的类型名称和私人字段的名称,您可以获得这样的值:

object o = typeof(Foo).GetField(
    "_secret",
    BindingFlags.Instance | BindingFlags.NonPublic
).GetValue(f);
Console.WriteLine(o); // writes "all your base are belong to us" to the console

当然,我总能看到你所有的私人领域

var fields = typeof(Foo).GetFields(
    BindingFlags.Instance | BindingFlags.NonPublic
);

答案 1 :(得分:2)

是的,有可能。硬编码值将出现在IL中,并且可以通过任何.NET反汇编程序查看。由于这是一个字段,因此可以在Reflector的构造函数中查看它从文字中初始化。