mercredi 7 octobre 2015

How can we get compiler generated field value by reflection?

I have a function in C#, which created a lambda delegate:

static public class PortableDemo
{
    static public void PortableKernel(int offset)
    {
        PortableUtil.Foo(() => Console.WriteLine("1"));
    }

I analyze the IL code, and it looks like:

IsStatic: true
             00000000 nop             
             00000001 ldsfld          System.Action <>9__0_0
             00000006 dup             
             00000007 brtrue.s        L_0020 /* = 32 (rel.: 23) */
             00000009 pop             

So, it loads a static field which name is <>9__0_0. This is a compiler generated field, and from dotPeek, I can confirm it is there by in gray color (means compiler generated private field).

Now the question is, I can get the FieldInfo of that field, but I try to get the value of it by fieldInfo.GetValue(null), and it always returns null. So how can I get that value?





Aucun commentaire:

Enregistrer un commentaire