System.Reflection.CustomReflectionExtensions.SetValueDirect C# (CSharp) Method

SetValueDirect() static private method

static private SetValueDirect ( this field, TypedReference obj, object value ) : void
field this
obj TypedReference
value object
return void
        internal static void SetValueDirect(this FieldInfo field, TypedReference obj, object value) // TODO: Replace with FieldInfo.SetValueDirect when available
        {
            if (field == null)
            {
                throw new NullReferenceException();
            }
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            // This is extremely inefficient, but without runtime support, we can't do much better.

            object[] values = new object[obj._fields.Length + 1];

            values[0] = obj._target;
            for (int i = 0; i < obj._fields.Length; i++)
            {
                values[i + 1] = obj._fields[i].GetValue(values[i]);
            }

            field.SetValue(values[values.Length - 1], value);

            for (int i = values.Length - 2; i >= 0; i--)
            {
                obj._fields[i].SetValue(values[i], values[i + 1]);
            }
        }
    }
CustomReflectionExtensions