Tech Off Post

Single Post Permalink

View Thread: Static initializers and reflection
  • User profile image

    So if you have a class that contains static fields, there is a static method that gets called to initialize these fields at startup (before any class instances are created). If you look at the IL, the method looks like this:

      .method private hidebysig specialname rtspecialname static 
              void  .cctor() cil managed
        // Code size       11 (0xb)
        .maxstack  8
        .line 25,25 : 9,42 ''
        IL_0000:  ldc.i4     0x3039
        IL_0005:  stsfld     int32 TestIL.TestClass1::m_int
        .line 16707566,16707566 : 0,0 ''
        IL_000a:  ret
      } // end of method TestClass1::.cctor

    My question is, how can one get this method via reflection at runtime?