I cannot figure out why this is not working. Can anyone explain to me why SetValue does not set the value of my object's properties?
T row = new T();
foreach (PropertyInfo property in row.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Field[] dbFields = property.GetCustomAttributes(typeof(Field), false) as Field[];
Object obj = (Int32)1000; // Arbitrary Value;
Object castObj = Convert.ChangeType(obj, property.PropertyType);
property.SetValue(row, castObj, null); // DOES NOT UPDATE row!! WHY!?
}
Any help is appreciated.
It should work fine so long as T
is a reference type (a class). It won't work as-is if T
is a value type (a struct). The value will be boxed on each call to property.SetValue
, and the existing value would remain untouched. Is it possible that that's the problem? If so, I'd urge you to avoid writing mutable value types in the first place - consider making it a reference type instead.
You could just force it to be boxed once, then unboxed once at the end - but if I were you I'd just steer well clear of mutable value types.