Skip to main content

Generics

Use zlua.make_generic_type to get a closed generic type table, then construct and call members like any other type. For arrays, see Arrays.

Closed generic types

Open definitions must include the backtick arity: List`1, Dictionary`2.

local ListInt = zlua.make_generic_type(
CSharp.mscorlib['System.Collections.Generic.List`1'],
zlua.types.int32
)

local list = ListInt()
list:Add(10)
list:Add(20)
local DictStrInt = zlua.make_generic_type(
CSharp.mscorlib['System.Collections.Generic.Dictionary`2'],
zlua.types.string,
zlua.types.int32
)

local dict = DictStrInt()
dict:Add("hp", 100)

The first argument is the generic definition type table; the rest are type arguments (zlua.types.*, typeof results, etc.).

Generic methods

When the method itself has type parameters (e.g. void Foo<T>(T a)), the first argument must be a generic_inst:

local inst = zlua.make_generic_inst(zlua.types.int32)
SomeType.Foo(inst, value)

Ordinary methods on already-closed types (e.g. List<int>.Add) do not use this path. See Type system §6.

Common mistakes

SymptomFix
Wrong arityCheck `1 / `2 vs argument count
List not foundUse full name System.Collections.Generic.List1` with bracket keys
Passing make_generic_inst to an already-closed methodOnly needed for open generic methods

Learning path

PreviousArrays
Nextref / in / out