Use reflection to set the value of a field in a structure that is part of an array of structures.

At the moment, my code is successfully setting the value of the fields / properties / arrays of the object using reflection, given the path to the field / property from the root object.

eg.

//MyObject.MySubProperty.MyProperty
SetValue('MySubProperty/MyProperty', 'new value', MyObject);

In the above example, the 'MyProperty' property of the MyObject object for 'new value'

I cannot use reflection to set the value of a field in a structure that is part of an array of structures, because struct is a value type (inside the array).

Here are some test classes / structures ...

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; }

The following shows how I successfully set the value of normal properties / fields and details / 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);

myObject.myStructArray [0].myField = '' " myObject.myOtherStructArray [1].myOtherField = '' "

, , "FieldInf.SetValue" (foundObject, newValue);

+3
4

( ).

, System.Array Array.SetValue .

+3

- / , , :

var property = this.GetType().GetProperty(myPropertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
ValueType vthis = this;
property.SetValue(vthis, myValue, null); // myValue is the value/object to be assigned to the property.
this = (UnderlyingsList)vthis;
+2

, , , , , :

    foundObject = //If element is [Blah] then get the
                  //object at the specified list position

() foundObject .

+1
source

My question went on ...

The only solution I found with a similar problem, I set the field / property property in the structure, which is the field, should have used ...

//GrandParentObject is myObject
//GrandParentType is typeof(MyClass)
//FieldIWantedToSet is the field info of myStruct.FieldIWantedToSet
FieldInfo oFieldValueTypeInfo = GrandParentType.GetField("myStruct");
TypedReference typedRefToValueType = TypedReference.MakeTypedReference(GrandParentObject, new FieldInfo[] { oFieldValueTypeInfo });
FieldIWantedToSet.SetValueDirect(typedRefToValueType, "my new value"); 

The problem is, how can I use SetValueDirect in an array / list of structures, I assume that my old method above will not work when the structures are in the array, because I cannot get FieldInfo for the structure (because it is in an array)?

0
source

All Articles