Generics
Use zlua.make_generic_type to get a closed generic type table, then construct and call members like any other type. For calling open generic methods such as GetComponent<T> only, start with Method calls. 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.*, type tables, etc.).
Generic methods
When the method itself has type parameters (e.g. T GetComponent<T>()), close them with zlua.inflate or call-site GTAP; this closes only the method's generic formals. If the declaring type is still open (e.g. List`1), call make_generic_type first, then inflate.
| Path | When |
|---|---|
zlua.inflate | Close by closed type + method name or full-signature key; cache the returned closure on hot paths |
| GTAP | Prefix N System.Type values (zlua.typeof(...)) before ordinary args on a short-name call; not type tables / strings / zlua.types.* |
-- inflate: full §3 typeArg (type table, string, System.Type, etc.)
local GetRb = zlua.inflate(GameObject, "GetComponent",
CSharp['UnityEngine.CoreModule']['UnityEngine.Rigidbody'])
local rb = GetRb(go)
-- GTAP: System.Type prefixes only
local Rigidbody = CSharp['UnityEngine.CoreModule']['UnityEngine.Rigidbody']
local rb2 = go:GetComponent(zlua.typeof(Rigidbody))
Ordinary methods on already-closed types (e.g. List<int>.Add) do not use this path. See Type system §6, 05-LIB §9.
After close, the return view follows the declared type; for a runtime-actual view see Return view type or zlua.cast. With multiple short-name candidates, you can pass a full-signature key as inflate's second argument — see Method overloads.
Common mistakes
| Symptom | Fix |
|---|---|
| Wrong arity | Check `1 / `2 vs argument count |
List not found | Use full name System.Collections.Generic.List1` with bracket keys |
Using inflate on a non-open-generic method | Only when the method itself has generic formals |
| Passing a type table to GTAP | Use zlua.typeof(typeTable), or use inflate |
Related
- Method calls — overloads / generic methods /
params - Return view type
- Method overloads
- zlua library Spec
- Type system
- Arrays