tags:

views:

14

answers:

1

I am working on a project where a field name from XML is loaded and the field with that name is returned and added to the current object. The problem is that FieldInfo.GetValue seems to return the current value of the field, not a reference. Is there any way to get around this?

+3  A: 

No unfortunately there is not. The design of FieldInfo.GetValue is to provide the value and not a reference. There is no other suitable method on FieldInfo to provide a reference either.

One reason why is that doing so would be simply unsafe. Imagen the scenario where the object is a struct on the stack. If a FieldInfo could provide a reference to a field of that struct then it would be supplying a reference to a piece of the stack which could go away at any moment. Reading or writing that reference after the stack went away would be incorrect and a type safety violation.

JaredPar
Ah, okay. Thanks for the help!
macfanpro