T
TEK
Hello
Have anyone succesfully used the SetValueDirect(TypedReference, object)
method?
I'm having a object wich again holds a struct that holds the objects
internal data.
I need to set a object value on the struct.
A way to do this for a value field would be:
_subfield.SetValue(_field.GetValue(obj), value);
where _field is a FieldInfo for the struct within the class, and _subField
is a FieldInfo for the actual field to set the value for.
However, if I the value to set is a object (not derivied from ValueType),
this will not work.
The _field.GetValue(obj) method will return a copy of the actual object, so
the _subfield.SetValue method will only modify the copy. The original object
will stay untouched.
A possible workaround is the following:
object holderObj = _field.GetValue(obj);
_subfield.SetValue(holderObj, value);
_field.SetValue(obj, holderObj);
this work for all types, however as this copy the whole struct into the
memory, and then back to the class, if the struct contains a lot of data
(witch it often will), this will lead to a very large overhead.
According to the documentation, the following should do exactly what I want,
however, I'm not able to make it work, it's telling me that "non-static
methods needs an object"
FieldInfo[] fieldArray = new FieldInfo[]{_field, _subfield};
targetref = TypedReference.MakeTypedReference(obj, fieldArray);
_subfield.SetValueDirect(targetref, value);
Any input would be nice....
Regards, TEK
Have anyone succesfully used the SetValueDirect(TypedReference, object)
method?
I'm having a object wich again holds a struct that holds the objects
internal data.
I need to set a object value on the struct.
A way to do this for a value field would be:
_subfield.SetValue(_field.GetValue(obj), value);
where _field is a FieldInfo for the struct within the class, and _subField
is a FieldInfo for the actual field to set the value for.
However, if I the value to set is a object (not derivied from ValueType),
this will not work.
The _field.GetValue(obj) method will return a copy of the actual object, so
the _subfield.SetValue method will only modify the copy. The original object
will stay untouched.
A possible workaround is the following:
object holderObj = _field.GetValue(obj);
_subfield.SetValue(holderObj, value);
_field.SetValue(obj, holderObj);
this work for all types, however as this copy the whole struct into the
memory, and then back to the class, if the struct contains a lot of data
(witch it often will), this will lead to a very large overhead.
According to the documentation, the following should do exactly what I want,
however, I'm not able to make it work, it's telling me that "non-static
methods needs an object"
FieldInfo[] fieldArray = new FieldInfo[]{_field, _subfield};
targetref = TypedReference.MakeTypedReference(obj, fieldArray);
_subfield.SetValueDirect(targetref, value);
Any input would be nice....
Regards, TEK