At the moment my code successfully sets the value of fields/properties/arrays of an object using reflection given a path to the field/property from the root object.
e.g.
//MyObject.MySubProperty.MyProperty
SetValue('MySubProperty/MyProperty', 'new value', MyObject);
The above example would set 'MyProperty' property of the 'MyObject' object to 'new value'
I'm unable to use reflection to set a value of a field in a struct which is part of an array of structs because the struct is a value type (within an array).
Here are some test classes/structs...
public class MyClass {
public MyStruct[] myStructArray = new MyStruct[] {
new MyStruct() { myField = "change my value" }
};
public MyStruct[] myOtherStructArray = new MyStruct[] {
new MyStruct() { myOtherField = "change my value" },
new MyStruct() { myOtherField = "change my other value" }
};
}
public struct MyStruct { public string myField; public string myOtherField; }
Below is how I successfully set the value of normal properties/fields and props/fields in lists...
public void SetValue(string pathToData, object newValue, object rootObject)
{
object foundObject = rootObject;
foreach (string element in pathToData.Split("/"))
{
foundObject = //If element is [Blah] then get the
//object at the specified list position
//OR
foundObject = //Else get the field/property
}
//Once found, set the value (this is the bit that doesn't work for
// fields/properties in structs in arrays)
FieldInf.SetValue(foundObject, newValue);
}
object myObject = new MyClass();
SetValue("/myStructArray/[0]/myField", "my new value", myObject);
SetValue("/myOtherStructArray/[1]/myOtherField", "my new value", myObject);
After that I want the myObject.myStructArray[0].myField = ''my new value" and myObject.myOtherStructArray[1].myOtherField = ''my new value"
All I need is a replacement for the 'FieldInf.SetValue(foundObject, newValue);' line
thanks in advance