14

Is there a way to set a single value in an array property via reflection in c#?

My property is defined like this:

double[]    Thresholds      { get; set; }

For "normal" properties I use this code to set it via reflection:

PropertyInfo pi = myObject.GetType().GetProperty(nameOfPropertyToSet);
pi.SetValue(myObject, Convert.ChangeType(valueToSet, pi.PropertyType), null);

How would I have to change this code to set the value in an array property at an arbitrary position? Thanks!

BTW: I tried to use the index parameter, but that seems only to work for indexed properties, not properties that are arrays...

3 Answers 3

23

When you do:

obj.Thresholds[i] = value;

that is semantically equivalent to:

double[] tmp = obj.Thresholds;
tmp[i] = value;

which means you don't want a SetValue at all; rather, you want to use GetValue to obtain the array, and then mutate the array. If the type is known to be double[], then:

double[] arr = (double[]) pi.GetValue(myObject, null);
arr[i] = value;

otherwise perhaps the non-generic IList approach (since arrays implement IList):

IList arr = (IList) pi.GetValue(myObject, null);
arr[i] = value;

If it is a multi-dimensional array, you'll have to use Array in place of IList.

Sign up to request clarification or add additional context in comments.

Comments

9

You are not actually setting the property, just changing the property value:

object value = myObject.GetType().GetProperty(nameOfPropertyToset).GetValue(myObject, null);

if (value is Array)
{
    Array arr = (Array)value;
    arr.SetValue(myValue, myIndex);
}
else
{
    ...
}

1 Comment

"changing the property value" is confusing phrasing, as that is synonymous with "setting the property" (directly or indirectly). Actually, the property value here never changes (it is the same reference); it is the array contents that change. Also, GetValue needs a 2nd parameter here.
0

Te code here will work for on it, its design to fill a 10 position array, you can do it for any size array.

The function PopulateInstance populate a data structure with what I need.

    object singleval;
    Array arrayval;
    System.Type LocalPType = obj.GetType().GetField(node.Name).FieldType;
    if (LocalPType.IsArray)
    {
        singleval = TreeNodeProperty.CreateNewInstance(LocalPType.GetElementType());
        arrayval =  Array.CreateInstance(LocalPType, 10);
        for(int i = 0; i < 10; i++)
        {
            singleval = PopulateInstance(singleval, node);
            arrayval.SetValue(singleval, i);
        }
        obj.GetType().GetField(node.Name).SetValue(obj, arrayval);
    }
    else
    {
        object val;
        val = Activator.CreateInstance(LocalPType);
        obj.GetType().GetField(node.Name).SetValue(obj,         PopulateInstance(val, node));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.