C# : FieldInfo.GetValue returns null

Posted by Florian on Stack Overflow See other posts from Stack Overflow or by Florian
Published on 2011-01-04T16:46:31Z Indexed on 2011/01/04 16:54 UTC
Read the original article Hit count: 195

Filed under:
|

Hi and Happy New year !

I've a problem to retrieve my control f2 in the variable o via Reflection :

public partial class Form1 : Form
{
    private Form2 f2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Type controlType = this.GetType();
        FieldInfo f = controlType.GetField("f2", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        object o = f.GetValue(this); // o == null;
    }
}

Thank you for your help !

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection