Is there a way to set properties on struct instances using reflection?

The value of rectangle is being boxed – but then you’re losing the boxed value, which is what’s being modified. Try this:

Rectangle rectangle = new Rectangle();
PropertyInfo propertyInfo = typeof(Rectangle).GetProperty("Height");
object boxed = rectangle;
propertyInfo.SetValue(boxed, 5, null);
rectangle = (Rectangle) boxed;

Leave a Comment