Skip to main content

Type system overview

:::tip Who should read this Developers already accessing C# from Lua who need to understand the CSharp table structure and static/instance isolation. Intro syntax: Lua accessing C# basics; API lookup: CSharp root table reference. :::

Design goals

GoalNotes
Unified entryAll types lazy-load through the CSharp root table
Semantics close to C#Type() construct, obj:Method(), Type.Static()
Static/instance isolationStatic and instance members use separate metadata tables
Public onlyLua can only access public members
OptimizableIl2Cpp fields / no-arg properties can take fast paths

CSharp table structure

CSharp -- global root table
└─ {assemblyName} -- assembly table (e.g. Assembly-CSharp)
└─ {typeFullName} -- type table (bracket key when namespaced)
├─ static methodTable / fieldGetter / fieldSetter
├─ _ctor / __call -- construct
└─ __instance_mt -- points at instance metatable template

Assembly alias (Demo convention):

CSharp['AC'] = CSharp['Assembly-CSharp']
local demo = CSharp.AC.Demo()

Static/instance isolation

Static and instance members must not be mixed:

OperationCorrectWrong
Static methodCSharp.AC.Demo.Add(1, 2)demo.Add(1, 2)
Instance methoddemo:GetX()CSharp.AC.Demo.GetX()
Static fieldCSharp.AC.Demo.s_x = 1demo.s_x = 1

:::info Exception Static and instance use separate tables; generally no mixing. To precisely name a static overload, use the full-signature key on the type table, e.g. Demo['Add(System.Int32,System.Int32)'](1, 2). :::

Lazy load & EnsureBinding

On first access to a type, the runtime scans public members and fills the three tables:

Il2Cpp: Generate stubs + lazy bind; Mono: Expression Emit + lazy bind.

Namespaces & generics (summary)

ScenarioSyntax
No namespaceCSharp.AC.Demo
With namespaceCSharp.AC['MyGame.UI.Panel']
Open genericCSharp.mscorlib['System.Collections.Generic.List1']`
Closed genericzlua.make_generic_type(ListDef, zlua.types.int32)

Details: Generics & arrays Guide.

When to read the Spec

QuestionDoc
How are members dispatched?Metatable model
Overloads & aliases?Method overload Spec
Arrays / inheritance?Type system Spec