Skip to content

Game.Reflection.FieldAccessor

Assembly:
Game (assembly name assumed from namespace; adjust if different)

Namespace:
Game.Reflection

Type:
public class

Base:
System.Object, IValueAccessor, IEquatable

Summary:
FieldAccessor is a small reflection helper that implements IValueAccessor for a specific FieldInfo on a parent accessor. It delegates GetValue/SetValue calls to the parent accessor and reads/writes the specified reflected field. When the parent value is a value type (struct), SetValue ensures updated boxed struct instances are written back to the parent so changes to fields are preserved.


Fields

  • private readonly IValueAccessor m_Parent
    The parent accessor that provides the object instance which contains the target field. Must be non-null. Used to obtain and (when necessary) write back the parent object when modifying fields, especially for value types.

  • private readonly System.Reflection.FieldInfo m_Field
    The reflected FieldInfo that this accessor reads from and writes to. Must be non-null.

Properties

  • public System.Type valueType { get; }
    Returns the Type of the field exposed by this accessor (m_Field.FieldType). This describes the type of values returned by GetValue and accepted by SetValue.

  • public IValueAccessor parent { get; }
    Returns the parent IValueAccessor (m_Parent) used to obtain the instance that owns the field.

Constructors

  • public FieldAccessor(IValueAccessor parent, System.Reflection.FieldInfo field)
    Creates a new FieldAccessor for the given parent accessor and FieldInfo. Throws ArgumentNullException if parent or field is null.

Methods

  • public object GetValue()
    Retrieves the parent value via m_Parent.GetValue() and returns the value of m_Field on that parent instance (m_Field.GetValue(parentValue)). If the parent is null or the field access fails, the underlying reflection call behavior applies (may throw).

  • public void SetValue(object value)
    Sets the field value on the parent instance (m_Field.SetValue(parentValue, value)). If the parent's value type (parent.valueType) is a value type (struct), the method calls m_Parent.SetValue(parentValue) after setting the field so that modifications to the boxed struct are propagated back to the parent accessor.

  • public bool Equals(FieldAccessor other)
    Type-specific equality: returns true if other is the same instance or if m_Parent.Equals(other.m_Parent) and m_Field.Equals(other.m_Field). Returns false for null.

  • public override bool Equals(object obj)
    Standard override that checks type and forwards to Equals(FieldAccessor).

  • public override int GetHashCode()
    Combines the parent's hash code and the field's hash code ((m_Parent.GetHashCode() * 397) ^ m_Field.GetHashCode()).

Usage Example

using System;
using System.Reflection;
using Game.Reflection;

// Minimal IValueAccessor wrapper for a root object instance.
class ObjectAccessor : IValueAccessor
{
    private object _value;
    public ObjectAccessor(object value) => _value = value;
    public Type valueType => _value?.GetType() ?? typeof(object);
    public object GetValue() => _value;
    public void SetValue(object value) => _value = value;
}

// Example target class with a private field.
class MyClass
{
    private int m_counter = 10;
}

var instance = new MyClass();
var rootAccessor = new ObjectAccessor(instance);

// Get FieldInfo for the private field (instance, non-public).
FieldInfo fieldInfo = typeof(MyClass).GetField("m_counter", BindingFlags.Instance | BindingFlags.NonPublic);

// Create the FieldAccessor
var fieldAccessor = new FieldAccessor(rootAccessor, fieldInfo);

// Read the private field
object current = fieldAccessor.GetValue(); // 10

// Write the private field
fieldAccessor.SetValue(42);
object updated = fieldAccessor.GetValue(); // 42

Notes: - FieldAccessor relies on IValueAccessor for obtaining and, when necessary, storing the parent instance. This allows chaining accessors for nested fields/properties. - When the parent accessor exposes a value type (struct), SetValue will update the boxed struct and call parent.SetValue(...) to ensure the modified struct is stored back into the parent container.