Skip to main content

CSharp Root Table

The global CSharp table is the sole entry for Lua to access all managed types. Assemblies and types are lazy-loaded: metatables and member bridges register on first access.

Access Syntax

CSharp -- root table
CSharp['Assembly-CSharp'] -- assembly (simple name, no .dll)
CSharp.AC.Demo -- no-namespace type (set alias first: CSharp['AC'] = CSharp['Assembly-CSharp'])
CSharp.AC['MyGame.UI.Panel'] -- namespaced type (brackets required)
Level__index behavior
CSharpLazy-load assembly table
{assembly}Lazy-load type table
{type}Static member table + _ctor / __call

Short alias (Demo convention, not required by the framework):

CSharp['AC'] = CSharp['Assembly-CSharp']

Type Table Capabilities

OperationSyntaxDescription
Construct instanceCSharp.AC.Demo()Calls _ctor / __call
Static fieldCSharp.AC.Demo.s_x = 10Via fieldGetter / fieldSetter
Static methodCSharp.AC.Demo.Add(3, 5)methodTable
Static propertyType.propSame syntax as fields (no args)
enum constantCSharp.AC['MyGame.Color'].Redinteger/number constant field

Instance Userdata

local demo = CSharp.AC.Demo()
demo.x = 10
demo:GetX()
demo:Run(10)

Instance members dispatch via the instance metatable (IMT), isolated from the type table's statics: do not use obj.StaticMethod() to reach static members.

Three-Table Dispatch (Strict Miss)

Member lookup order:

TableRead (__index)Write (__newindex)
methodTableMethods, dispatch, aliases
fieldGetterTableFields, parameterless properties
fieldSetterTableFields, parameterless properties

No reflection fallback: unregistered members raise a Lua error immediately, so API misuse surfaces early.

Lazy-Load Flow

Il2Cpp Player pre-generates bindings at codegen; Mono Editor reflects + caches at runtime.

Common Error Messages

Message (example)Cause
type not found / assembly nilWrong assembly name, type not public, or Player omits the assembly
member 'X' not foundMember not public, typo, or Il2Cpp bridge not generated
Dot access fails for namespaced typeUse CSharp.asm['Ns.Type'] bracket form
invalid userdataInstance released or type mismatch

Troubleshooting: troubleshooting.

Mono / Il2Cpp Differences (Lua-Visible Semantics Match)

ItemMonoIl2Cpp
Lazy load✅ Reflect EnsureBinding✅ Generate stub + runtime bind
Field / property direct read
Overload dispatch / full-signature keys
Generics
Event (add_ / remove_)