Skip to content

Game.Reflection.GetterWithDepsAccessor

Assembly:
Assembly-CSharp (typical for Cities: Skylines 2 mods)

Namespace:
Game.Reflection

Type:
class GetterWithDepsAccessor

Base:
IValueAccessor, IEquatable

Summary:
GetterWithDepsAccessor is a readonly value accessor that wraps another IValueAccessor (the parent). It invokes a MethodInfo getter on the parent value and returns the result. Optionally the accessor can be provided with an array of parameters for the method invocation and an index pointing to a Unity.Jobs.JobHandle inside that parameters array; if that index is set, the JobHandle at that index will be Completed after the getter is invoked. Equality is based on the parent accessor and the getter MethodInfo.


Fields

  • private readonly IValueAccessor m_Parent
    Reference to the source/accessor that provides the target object on which the getter MethodInfo will be invoked. This is required and cannot be null.

  • private readonly MethodInfo m_Getter
    The MethodInfo representing the getter or method to invoke on the parent value. Its ReturnType defines the accessor's valueType. Required and cannot be null.

  • private readonly object[] m_Parameters
    Optional array of parameters passed to MethodInfo.Invoke. May be null. If a JobHandle dependency is used, it should be placed in this array at the index specified by m_DepsIndex.

  • private readonly int m_DepsIndex
    Index into m_Parameters that points to a Unity.Jobs.JobHandle which should be Completed after the getter invocation. A value of -1 means "no dependency".

Properties

  • public Type valueType { get; }
    Exposes the return type of the configured getter (m_Getter.ReturnType). This describes the type of value that GetValue will return.

Constructors

  • public GetterWithDepsAccessor([NotNull] IValueAccessor parent, [NotNull] MethodInfo getter, [CanBeNull] object[] parameters = null, int depsIndex = -1)
    Creates a new GetterWithDepsAccessor.
  • parent: the IValueAccessor that provides the instance to invoke the getter on (required).
  • getter: the MethodInfo to invoke on the parent value (required).
  • parameters: optional parameters to pass to MethodInfo.Invoke; may include a JobHandle.
  • depsIndex: index into parameters of a JobHandle to Complete after invocation; -1 to disable.

The constructor throws ArgumentNullException if parent or getter are null.

Methods

  • public object GetValue()
    Invokes m_Getter on the object returned by m_Parent.GetValue(), passing m_Parameters (if any), and returns the invocation result. After the invocation, if m_DepsIndex != -1 and m_Parameters is not null, the object at m_Parameters[m_DepsIndex] is cast to Unity.Jobs.JobHandle and Completed (JobHandle.Complete()) before returning. Note that the getter is invoked before the JobHandle is Completed (the code intentionally completes the dependency after invocation).

Potential exceptions: - Any exceptions thrown by m_Parent.GetValue() or MethodInfo.Invoke (including TargetInvocationException) propagate to the caller. - If the parent value is null and the getter is an instance method, Invoke may throw.

  • public void SetValue(object value)
    Always throws InvalidOperationException("GetterWithDepsAccessor is readonly"). This accessor is read-only and does not support setting values.

  • public bool Equals(GetterWithDepsAccessor other)
    Implements IEquatable. Two GetterWithDepsAccessor instances are considered equal if they refer to equal parent accessors and equal MethodInfo getter objects. Null and reference-equality checks are handled.

  • public override bool Equals(object obj)
    Standard override: returns true when obj is a GetterWithDepsAccessor and Equals(GetterWithDepsAccessor) returns true.

  • public override int GetHashCode()
    Hash code is computed from the parent's hash code and the getter's hash code: (m_Parent.GetHashCode() * 397) ^ m_Getter.GetHashCode().

Usage Example

// Example: wrapping a parent accessor and invoking a getter with a JobHandle dependency
IValueAccessor parentAccessor = /* an accessor that returns the object instance */;
MethodInfo getterMethod = typeof(MyType).GetMethod("GetResult"); // or another method
// Assume GetResult signature: ResultType GetResult(JobHandle handle)
JobHandle handle = /* some scheduled job handle */;
object[] parameters = new object[] { handle };

// depsIndex is 0 because parameters[0] is the JobHandle to Complete after invocation
var accessor = new GetterWithDepsAccessor(parentAccessor, getterMethod, parameters, depsIndex: 0);

// When calling GetValue, the getter is invoked and then the JobHandle at parameters[0] is Completed.
// The result of the getter is returned.
object result = accessor.GetValue();

Notes and tips: - The accessor is readonly; do not call SetValue. - Be careful about the order: the getter is invoked before the JobHandle is Completed in the current implementation. - Invocation can throw TargetInvocationException or other exceptions from the invoked method — callers should handle exceptions as appropriate.