跳到主要内容

常用 zlua 库

职责
CSharp程序集 / 类型 / 成员 / 构造
zlua类型构造辅助、opaque、数组、delegate、重载短名
实例 userdata成员经元表,不经 zlua

全文 API:05-LIB参考。本篇只列最常用。

类型实参

API用途
zlua.types.int32预置类型实参
zlua.typeof(typeTable)≡ C# typeof,返回 System.Type
zlua.get_type_from_name(name)对标 Type.GetType

重载

同名多候选时,Bind 已自动挂全签名键(如 Run(System.Int32)),一般 不必 先调库:

demo['Run(System.Int32)'](demo, 5)

需要短名 + 冒号时再 register_method

local run_i32 = demo['Run(System.Int32)']
zlua.register_method("run_i32", run_i32)
demo:run_i32(5) -- 注册后的好处:可读短名 + 冒号语法

zlua.signature(...) 只生成参数括号部分(如 "(System.Int32)"),用于对照/拼接;不能单独当元表键。见 方法重载

泛型与数组

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

local arr = zlua.new_szarray_by_element_type(zlua.types.int32, 4)
local t = zlua.to_table(arr)
local bytes = zlua.to_bytes(float_arr) -- blittable 元素

泛型数组

Opaque / ref

API用途
get_opaquevalue / set_opaquevalue同步链内读写 Opaque
to_user_dataOpaque 升级为 userdata 门面(见规范)

ref struct:传同型 Type(...) userdata 即可写回。见 ref/out/in

Delegate

local d = zlua.to_delegate(fn, ActionIntType) -- 第二参须已闭合

形参已是具体 Action/Func 时通常 不必 调用。见 Function

其它(需要时再查规范)

box / unbox / castmake_generic_method、mdarray 系列、Native __zlua_* —— 见 05-LIB

学习路径

上一篇Extension 方法
下一篇第三方原生插件

相关文档