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

Rob*_*ers 2 .net reflector disassembly

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

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

    public void Foo()
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

jas*_*son 5

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

class Foo { private string _secret = @"all your base are belong to us"; }
Run Code Online (Sandbox Code Playgroud)

转化为拥有构造函数

public Foo() { this._secret = "all your base are belong to us"; }
Run Code Online (Sandbox Code Playgroud)

Foo在方法中的反射器中可见.ctor

您还可以在ildasm(随 Microsoft Visual Studio 一起提供)中查看此信息Foo::.ctor : void

.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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

var fields = typeof(Foo).GetFields(
    BindingFlags.Instance | BindingFlags.NonPublic
);
Run Code Online (Sandbox Code Playgroud)