8
   class PriceClass {

        private int value;
        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }           
    }


    struct PriceStruct
    {

        private int value;
        public int Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
    }
    static void Main(string[] args)
    {
        PriceClass _priceClass = new PriceClass();
        Type type = typeof(PriceClass);
        PropertyInfo info = type.GetProperty("Value");
        info.SetValue(_priceClass, 32, null);
        Console.WriteLine(_priceClass.Value);

        PriceStruct _priceStruct = new PriceStruct();
        type = typeof(PriceStruct);
        info = type.GetProperty("Value");
        info.SetValue(_priceStruct, 32, null);
        Console.WriteLine(_priceStruct.Value);

        Debugger.Break();
    }

The first value printed is 32 while the second is 0. No exception thrown

1

3 Answers 3

13

It's because boxing your struct makes a copy of it, so you should box it earlier so you call the getter from the same data that you modified. The following code works:

    object _priceStruct = new PriceStruct(); //Box first
    type = typeof(PriceStruct);
    info = type.GetProperty("Value");
    info.SetValue(_priceStruct, 32, null);
    Console.WriteLine(((PriceStruct)_priceStruct).Value); //now unbox and get value

    Debugger.Break();
1
  • 1
    Thank you. This is the first time I've seen forced boxing (turning a struct into an object) and unboxing (turning the object back into a struct) to see what is happening and understand the process. It is simple, really. But everywhere else I looked, the boxing steps were all done by hidden processes (automatically by the compiler) and it made no sense how one could work with it.
    – Mark T
    Jul 30, 2015 at 18:28
5

structs are ValueTypes, which are passed by value, that means you only pass around copies of the entire struct, not a reference to the original object.

So when you pass it into info.SetValue(_priceStruct, 32, null), a copy is passed to the method and mutated, so the original object doesn't get changed at all. Another reason why mutable structs are evil.

1

You can still change them using reflection but it is a bit long winded.

See this example: http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/2dd4315c-0d0d-405c-8d52-b4b176997472

1
  • 2
    +1 Your example shows I think the best that can be done. It isn't possible in "safe" code to alter a field in a genuine value type using reflection, but for every value type there's a corresponding boxed class type. Casting to the ironically-named ValueType will convert a value type into an instance of its corresponding boxed class type, which may then be mutated using reflection; after mutation, the contents of the boxed instance can be copied back to a genuine value-type object.
    – supercat
    Jul 26, 2011 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.