Note that there are some explanatory texts on larger screens.

plurals
  1. POUse reflection to set the value of a field in a struct which is part of an array of structs
    text
    copied!<p>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.</p> <p>e.g.</p> <pre><code>//MyObject.MySubProperty.MyProperty SetValue('MySubProperty/MyProperty', 'new value', MyObject); </code></pre> <p>The above example would set 'MyProperty' property of the 'MyObject' object to 'new value'</p> <p>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).</p> <p>Here are some test classes/structs...</p> <pre><code>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; } </code></pre> <p>Below is how I successfully set the value of normal properties/fields and props/fields in lists...</p> <pre><code>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); </code></pre> <p>After that I want the myObject.myStructArray[0].myField = ''my new value" and myObject.myOtherStructArray[1].myOtherField = ''my new value"</p> <p>All I need is a replacement for the 'FieldInf.SetValue(foundObject, newValue);' line</p> <p>thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload