Reflection
Overview
Cera’s reflection API lets you inspect types, fields, methods, properties, and attributes at runtime. All reflection types live in the Core.Reflection namespace.
Getting Type Info
Use typeof() on a type name or instance:
using Core.Reflection;
immut ref Type t = typeof(Point); // compile-time typeimmut ref Type t2 = typeof(someObject); // runtime type (from GC header)Reflection.Type
Type describes a type’s structure:
| Property | Type | Description |
|---|---|---|
Name | string | Type name |
Size | int | Byte size of the type |
IsPrimitive | bool | Whether it is a primitive type |
FieldCount | int | Number of fields |
MethodCount | int | Number of methods |
PropertyCount | int | Number of properties |
BaseType | Type? | Parent type (null if none) |
| Method | Returns | Description |
|---|---|---|
GetField(int index) | Field | Get field by index |
GetMethod(int index) | Method | Get method by index |
GetProperty(int index) | Property | Get property by index |
Inherits(Type other) | bool | Whether this type inherits from other |
Reflection.Method
| Property | Type | Description |
|---|---|---|
Name | string | Method name |
ParameterCount | int | Number of parameters |
IsStatic | bool | Whether the method is static |
ReturnType | Type | Return type |
DeclaringType | Type? | Owning type |
| Method | Returns | Description |
|---|---|---|
GetParameter(int index) | Parameter | Get parameter by index |
Reflection.Parameter
Parameter describes a method parameter:
| Property | Type | Description |
|---|---|---|
Name | string | Parameter name |
HasDefaultValue | bool | Whether a default value exists |
IsByRef | bool | Whether passed by reference |
IsArray | bool | Whether the parameter is an array type |
IsNullable | bool | Whether the parameter is nullable |
ParameterType | Type | Parameter type |
using Core.Reflection;
type Greeter{ string Greet(string name, int times) { return name; }}
immut ref Method m = Greeter.Greet;for (int i = 0; i < m.ParameterCount; i++){ immut ref Parameter p = m.GetParameter(i); Console.WriteLine(p.Name);}Reflection.Property
| Property | Type | Description |
|---|---|---|
Name | string | Property name |
CanRead | bool | Has getter |
CanWrite | bool | Has setter |
CanAdd | bool | Has add accessor (+=) |
CanSub | bool | Has sub accessor (-=) |
IsStatic | bool | Whether the property is static |
PropertyType | Type | Property value type |
DeclaringType | Type? | Owning type |
Getter | Method? | Getter method info |
Setter | Method? | Setter method info (null if read-only) |
Adder | Method? | Add accessor method (+=) |
Subber | Method? | Sub accessor method (-=) |
Initer | Method? | Init accessor method |
Reflection.Field
| Property | Type | Description |
|---|---|---|
Name | string | Field name |
Offset | int | Byte offset in type layout |
FieldType | Type | Field value type |
DeclaringType | Type? | Owning type |
IsPublic | bool | Public visibility |
IsPrivate | bool | Private visibility |
IsProtected | bool | Protected visibility |
Compile-Time Member Access
Access reflection objects directly via Type.Member syntax:
using Core.Reflection;
type Calculator{ int Add(int a, int b) { return a + b; }}
immut ref Method m = Calculator.Add;int paramCount = m.ParameterCount; // 2Iterating Members
using Core.Reflection;
type Point { int x; int y; int z; }
immut ref Type t = typeof(Point);for (int i = 0; i < t.FieldCount; i++){ immut ref Field f = t.GetField(i); Console.WriteLine(f.Name);}Type Hierarchy
using Core.Reflection;
type Animal { }type Dog : Animal { }
immut ref Type t = typeof(Dog);immut ref Type parent = typeof(Animal);bool inherits = t.Inherits(parent); // trueAttribute Access
Use HasAttribute<#T> and GetAttribute<#T> to query attributes on methods, fields, and properties:
using Core.Reflection;
type #Deprecated{ string message;}
type Api{ #Deprecated("Use NewMethod instead") void OldMethod() { }}
// Check if a method has an attributeif (Api.OldMethod.HasAttribute<#Deprecated>){ Console.WriteLine("Method is deprecated");}
// Get attribute with its field values#Deprecated? dep = Api.OldMethod.GetAttribute<#Deprecated>;if (dep != null){ Console.WriteLine(dep.message); // "Use NewMethod instead"}HasAttribute and GetAttribute are available on Method, Property, and Field reflection objects.
Property Accessor Reflection
Access individual property accessors (get, set, add, sub, init) as Method objects using a three-component path:
using Core.Reflection;
type Counter{ int Value { get; =(v) prop = v; +=(v) prop = prop + v; -=(v) prop = prop - v; }}
immut ref Method getter = Counter.Value.get;immut ref Method setter = Counter.Value.set;immut ref Method adder = Counter.Value.add;immut ref Method subber = Counter.Value.sub;
Console.WriteLine(getter.Name); // "get_Value"Console.WriteLine(getter.ParameterCount); // 0Console.WriteLine(setter.ParameterCount); // 1You can also access accessors at runtime through Property.Getter, Property.Setter, Property.Adder, Property.Subber, and Property.Initer.