Frankly, there's no need whatsoever for TypedReference
here - just a boxed struct should work fine:
var amountField = obj.GetType().GetField("Amount");
object money = amountField.GetValue(obj);
var codeField = money.GetType().GetField("Code");
codeField.SetValue(money, "XXX");
amountField.SetValue(obj, money);
However! I will advise you of a few things:
- public fields instead of properties are not usually a good idea; that will often bite you later
- mutable structs (i.e. structs that can be changed after creation) are almost never a good idea, and will bite even more often, and bite harder
- combining mutable structs and public fields compounds it, but making it very problematic to change later