Game.Reflection.PropertyAccessor
Assembly: Assembly-CSharp
Namespace: Game.Reflection
Type: class
Base: implements IValueAccessor, IEquatable
Summary:
PropertyAccessor is a small reflection-backed accessor that reads and (optionally) writes a property value from an object obtained through a parent IValueAccessor. It stores the MethodInfo for the property's getter and optional setter and delegates GetValue/SetValue calls to those methods on the parent-provided object. The class uses [NotNull] / [CanBeNull] annotations on constructor arguments and fields.
Fields
-
private readonly IValueAccessor m_Parent
Holds the parent value accessor that supplies the target object instance on which the property getter/setter are invoked. This field is required and cannot be null (constructor throws ArgumentNullException for null parent). -
private readonly MethodInfo m_Getter
The MethodInfo representing the property's getter. This is required and cannot be null. The property's type is exposed via the valueType property which returns m_Getter.ReturnType. -
private readonly MethodInfo m_Setter
The MethodInfo for the property's setter, or null if the property is read-only or no setter was provided. If null, SetValue will be a no-op.
Properties
public Type valueType { get; }
Returns the value type of the property (m_Getter.ReturnType). Always derived from the getter MethodInfo.
Constructors
public PropertyAccessor(IValueAccessor parent, MethodInfo getter, MethodInfo setter)
Creates a PropertyAccessor. Arguments:- parent: IValueAccessor that supplies the instance on which the property is accessed (must not be null).
- getter: MethodInfo for the property's getter (must not be null).
- setter: MethodInfo for the property's setter, or null if the property is read-only. Throws ArgumentNullException if parent or getter is null.
Methods
-
public object GetValue()
Invokes the stored getter on the object returned by m_Parent.GetValue() and returns the result. This uses MethodInfo.Invoke, so any exceptions thrown by the invoked getter will propagate as TargetInvocationException wrappers or the original exception depending on invocation. If m_Parent.GetValue() returns null, invoking the getter will throw a NullReference/TargetException. -
public void SetValue(object value)
If a setter MethodInfo was provided (m_Setter != null), invokes the setter on the object returned by m_Parent.GetValue() with the given value. If m_Setter is null, this method does nothing (no exception). Like GetValue, invoking the setter may throw reflection invocation exceptions if the target is null or the invocation fails. -
public bool Equals(PropertyAccessor other)
Equality is defined by both the parent accessor and the getter MethodInfo. Returns true if: - other is the same reference, or
-
m_Parent.Equals(other.m_Parent) && m_Getter.Equals(other.m_Getter) If other is null, returns false.
-
public override bool Equals(object obj)
Standard override that checks type and forwards to Equals(PropertyAccessor). -
public override int GetHashCode()
Combines the parent's hash code and the getter's hash code: (m_Parent.GetHashCode() * 397) ^ m_Getter.GetHashCode().
Notes: - The class relies on the Equals implementation of the parent IValueAccessor for equality checks. - Reflection invocation is used (MethodInfo.Invoke), which has performance and security considerations; this class is intended for reflection-based access patterns and not for hot paths where performance is critical.
Usage Example
using System;
using System.Reflection;
using Game.Reflection;
// Minimal IValueAccessor implementation for example
public class SimpleAccessor : IValueAccessor
{
private object _value;
public SimpleAccessor(object value) { _value = value; }
public object GetValue() => _value;
public void SetValue(object value) { _value = value; }
}
public class MyClass
{
public int Number { get; set; }
}
public static void Example()
{
var instance = new MyClass { Number = 42 };
IValueAccessor parent = new SimpleAccessor(instance);
PropertyInfo propInfo = typeof(MyClass).GetProperty("Number", BindingFlags.Public | BindingFlags.Instance);
MethodInfo getter = propInfo.GetGetMethod(true); // not null
MethodInfo setter = propInfo.GetSetMethod(true); // not null
var accessor = new PropertyAccessor(parent, getter, setter);
// Read property
object value = accessor.GetValue(); // 42
// Write property
accessor.SetValue(100);
Console.WriteLine(((MyClass)parent.GetValue()).Number); // 100
}