Skip to content

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 type
immut ref Type t2 = typeof(someObject); // runtime type (from GC header)

Reflection.Type

Type describes a type’s structure:

PropertyTypeDescription
NamestringType name
SizeintByte size of the type
IsPrimitiveboolWhether it is a primitive type
FieldCountintNumber of fields
MethodCountintNumber of methods
PropertyCountintNumber of properties
BaseTypeType?Parent type (null if none)
MethodReturnsDescription
GetField(int index)FieldGet field by index
GetMethod(int index)MethodGet method by index
GetProperty(int index)PropertyGet property by index
Inherits(Type other)boolWhether this type inherits from other

Reflection.Method

PropertyTypeDescription
NamestringMethod name
ParameterCountintNumber of parameters
IsStaticboolWhether the method is static
ReturnTypeTypeReturn type
DeclaringTypeType?Owning type
MethodReturnsDescription
GetParameter(int index)ParameterGet parameter by index

Reflection.Parameter

Parameter describes a method parameter:

PropertyTypeDescription
NamestringParameter name
HasDefaultValueboolWhether a default value exists
IsByRefboolWhether passed by reference
IsArrayboolWhether the parameter is an array type
IsNullableboolWhether the parameter is nullable
ParameterTypeTypeParameter 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

PropertyTypeDescription
NamestringProperty name
CanReadboolHas getter
CanWriteboolHas setter
CanAddboolHas add accessor (+=)
CanSubboolHas sub accessor (-=)
IsStaticboolWhether the property is static
PropertyTypeTypeProperty value type
DeclaringTypeType?Owning type
GetterMethod?Getter method info
SetterMethod?Setter method info (null if read-only)
AdderMethod?Add accessor method (+=)
SubberMethod?Sub accessor method (-=)
IniterMethod?Init accessor method

Reflection.Field

PropertyTypeDescription
NamestringField name
OffsetintByte offset in type layout
FieldTypeTypeField value type
DeclaringTypeType?Owning type
IsPublicboolPublic visibility
IsPrivateboolPrivate visibility
IsProtectedboolProtected 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; // 2

Iterating 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); // true

Attribute 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 attribute
if (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); // 0
Console.WriteLine(setter.ParameterCount); // 1

You can also access accessors at runtime through Property.Getter, Property.Setter, Property.Adder, Property.Subber, and Property.Initer.