Skip to content

Reflection API

Overview

The Cera Reflection API lets programs inspect types, fields, and methods at runtime.

Getting Type Information

Use the typeof() operator to get a Reflection.Type:

Reflection.Type t = typeof(Point)
Console.WriteLine(t.Name)
Console.WriteLine(t.QualifiedName)
Console.WriteLineInt(t.Size)
Console.WriteLine($"{t.IsAbstract}")
Console.WriteLine($"{t.IsFinal}")

Reflection.Type Properties

PropertyTypeDescription
t.NamestringShort type name
t.QualifiedNamestringFully qualified name
t.SizeintSize in bytes
t.BaseTypeReflection.TypeBase type (if any)
t.IsAbstractboolWhether abstract
t.IsFinalboolWhether final

Counting Members

Console.WriteLineInt(t.MethodCount)
Console.WriteLineInt(t.PropertyCount)
Console.WriteLineInt(t.FieldCount)

Looking Up Members

Reflection.Method m = t.GetMethod("DoWork")
Reflection.Property p = t.GetProperty("Name")
Reflection.Field f = t.GetField("x")

Reflection.Method

Reflection.Method m = typeof(Calculator).GetMethod("Add")
Console.WriteLine(m.Name)
Console.WriteLine($"{m.IsStatic}")
Console.WriteLine($"{m.IsVirtual}")
Console.WriteLineInt(m.ParameterCount)
Reflection.Type ret = m.ReturnType
Console.WriteLine(ret.Name)

Reflection.Field

Reflection.Field f = typeof(Point).GetField("x")
Console.WriteLine(f.Name)
Console.WriteLineInt(f.Offset)
Console.WriteLine($"{f.IsPublic}")
Reflection.Type ft = f.FieldType
Console.WriteLine(ft.Name)

Example

void PrintTypeInfo(Reflection.Type t)
{
Console.WriteLine($"Type: {t.Name}");
Console.WriteLine($" Size: {t.Size}");
Console.WriteLine($" Methods: {t.MethodCount}");
Console.WriteLine($" Fields: {t.FieldCount}");
}
PrintTypeInfo(typeof(Point));