Game.UI.Widgets.WidgetReflectionUtils
Assembly: Game
Namespace: Game.UI.Widgets
Type: public static class
Base: System.Object
Summary:
Utility helpers for working with reflected widget member types and names. Provides helpers to detect list/array types and element types, create FieldBuilder delegates for Field
Fields
private static CustomSerializationPolicy kListElementLabelPolicy
Custom serialization policy named "ListElementLabel" that allows non-serializable types. It matches members that have the ListElementLabelAttribute and a return type of string. Used by GetListElementLabelMember to locate a member that should be used as the display label for list elements.
Properties
- None (this static utility class exposes only methods and a private field).
Constructors
- N/A — static class (no public constructors)
Methods
-
public static bool IsListType(Type memberType)
Determines if the provided type represents a list-like type. Returns true for arrays or types assignable from IList that also implement a generic IListinterface (e.g., IList , List ). -
[CanBeNull] public static Type GetListElementType(Type memberType)
If memberType is an array, returns its element type. If it implements a generic IList, returns the generic argument T. Returns null if the type is not an array or a generic list interface. -
private static bool IsGenericListInterface(Type type)
Private helper that checks whether a given interface type is a generic IList<> interface (compares the generic type definition to typeof(IList<>)). -
public static FieldBuilder CreateFieldBuilder<T, U>() where T : Field<U>, new()
Creates and returns a FieldBuilder delegate that constructs a new T (which must inherit Field and have a parameterless constructor) and sets its accessor to a CastAccessor wrapping the provided IValueAccessor. Use this when no custom object conversion is required. -
public static FieldBuilder CreateFieldBuilder<T, U>(Converter<object, U> fromObject, Converter<U, object> toObject) where T : Field<U>, new()
Like the parameterless CreateFieldBuilder, but supplies conversion delegates for wrapping the IValueAccessor with a CastAccessor that performs conversions between object and U. -
public static string NicifyVariableName(string name)
Converts internal variable or property names into a nicer, human-readable label. Behaviors: - Removes common prefixes "m_" and "Get"
- Inserts spaces before camel-case boundaries and digits
- Upper-cases the first character of the resulting string
-
Returns an empty string for null input
-
public static MemberInfo GetListElementLabelMember(Type type)
Uses FormatterUtilities.GetSerializableMembers with the kListElementLabelPolicy to find members on the given type that have the ListElementLabelAttribute and return type string, then returns the first match (or null). Useful to locate a member that should be used as a label for items in a list UI.
Usage Example
using System;
using System.Collections.Generic;
using Game.UI.Widgets;
public class ExampleUsage
{
public void Demo()
{
// Nicify variable names
string label = WidgetReflectionUtils.NicifyVariableName("m_myAwesomeValue1");
// label == "My Awesome Value 1"
// Check list types and get element type
bool isList = WidgetReflectionUtils.IsListType(typeof(List<int>)); // true
Type elemType = WidgetReflectionUtils.GetListElementType(typeof(List<int>)); // typeof(int)
// Create a FieldBuilder for a custom Field<int> subclass
// (Assumes IntField : Field<int> exists and FieldBuilder/IValueAccessor are in scope)
FieldBuilder builder = WidgetReflectionUtils.CreateFieldBuilder<IntField, int>();
// builder can now be used to create an IntField given an IValueAccessor
}
}
Notes:
- This utility uses Colossal.OdinSerializer utilities (FormatterUtilities, CustomSerializationPolicy) and attributes (ListElementLabelAttribute).
- FieldBuilder, Field