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
| Property | Type | Description |
|---|---|---|
t.Name | string | Short type name |
t.QualifiedName | string | Fully qualified name |
t.Size | int | Size in bytes |
t.BaseType | Reflection.Type | Base type (if any) |
t.IsAbstract | bool | Whether abstract |
t.IsFinal | bool | Whether 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.ReturnTypeConsole.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.FieldTypeConsole.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));