Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a value through reflection is not working

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?

like image 843
Scott Chamberlain Avatar asked Feb 02 '26 10:02

Scott Chamberlain


2 Answers

I think headers might be getting boxed into a new object since it's a struct, then the object is getting garbage collected as soon as SetValue returns. Change it to a class and see if the problem goes away.

like image 190
Rodrick Chapman Avatar answered Feb 03 '26 23:02

Rodrick Chapman


SetValue is expecting an object which causes a boxing operation on headers. As headers is a struct, it is a value type. Therefore a copy is made and what you are modifying is the boxed object and not headers.

You should seriously consider avoiding mutable value types.

From Eric Lippert:

This is yet another reason why mutable value types are evil. Try to always make value types immutable.

like image 30
jason Avatar answered Feb 04 '26 00:02

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!