Is it possible to see a variable with a variable hardcoded using a disassembler like Reflector?

Given the source code example below, is it possible for someone to see the value _secret

using a disassembler? I haven't seen a way to get the value through Reflector, but I haven't used it very much. Let's assume the code is not obfuscated in any way.

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

    public void Foo()
    {
        ...
    }
}

      

Thank!

+2


source to share


2 answers


It displays in the constructor in Reflector.

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

      

translates to constructor

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

      

which is displayed in Reflector under Foo

the method .ctor

.

You can also see this information in ildasm

(comes with Microsoft Visual Studio) at 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

      

Finally, if someone knows the name of your type and the name of your private field, you can get the value as such:

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

      

Of course, I can always see all of your personal fields with

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

      

+4


source


Yes it is possible. The hard-coded value will be present in the IL and will be viewable through any .NET disassembler. Since this is a field, its initialization from a literal will show up in the constructor in Reflector.



+2


source







All Articles