Setting a value through reflection is not working.

Posted by Scott Chamberlain on Stack Overflow See other posts from Stack Overflow or by Scott Chamberlain
Published on 2010-01-07T19:46:28Z Indexed on 2010/03/25 20:13 UTC
Read the original article Hit count: 415

Filed under:
|
|

I am trying to set a value through reflection. I created this little test program

    struct headerIndexes
    {
        public int AccountNum{ get; set; }
        public int other { get; set; }
        public int items { get; set; }
    }
    static void Main(string[] args)
    {

        headerIndexes headers = new headerIndexes();
        headers.AccountNum = 1;
        Console.WriteLine("Old val: {0}", headers.AccountNum);
        foreach (var s in headers.GetType().GetProperties())
        {
            if (s.Name == "AccountNum")
                s.SetValue(headers, 99, null);
        }
        Console.WriteLine("New val: {0}", headers.AccountNum);
        Console.ReadKey();
    }

Steping thorugh the program i see it correctly does the command s.SetValue(headers, 99, null); however the value of headers.AccountNum stays at 1 when setValue is run.

Am I missing a obvious step?

© Stack Overflow or respective owner

Related posts about reflection

Related posts about c#