Skip to main content

LuaAlias

Give a C# method a final Lua-side key name (rename, not “default name plus an extra alias”). When an alias exists, that method is no longer registered under MethodInfo.Name. A single candidate is direct (O(1)), suitable for hot paths.

Authoritative: Overload Spec §5. Compare with other overload strategies in Method overloads.

When to use

ScenarioApproach
You can edit C#; hot path wants a short name + colon[LuaAlias]
Cannot change source (precompiled DLL)Settings luaAliasXmlPaths + ZLuaAlias XML
Cannot change C#; temporary runtime namezlua.register_method (see Overloads)
Exact pick; syntax does not matterFull-signature key demo['Run(System.Int32)'](demo, 5)

[LuaAlias] attribute

using ZLua;

public class Demo
{
[LuaAlias("run_i32")]
public void Run(int value) { }

public void Run(string value) { }
}
local demo = CSharp.AC.Demo()
demo:run_i32(10) -- 命中 Run(int);该方法不再挂名为 "Run"
demo:Run("hi") -- 仅剩 Run(string) 等未换名的重载 → 此处为 direct 或 dispatch
ItemNotes
TargetMethod only
Per methodAt most one alias (AllowMultiple = false)
InheritanceDoes not inherit to subclass overrides
SemanticsReplaces the default Lua name; does not append

Name collisions (allowed)

An alias may match another method’s default name or another alias → they join the same overload group and calls use dispatch:

public void Foo(int x) { }

[LuaAlias("Foo")] // Bar 换名为 Foo,并入 "Foo" 组;不再挂 "Bar"
public void Bar(string s) { }

[LuaAlias("print")]
public void LogA(int x) { }

[LuaAlias("print")] // 两个 print → dispatch
public void LogB(string s) { }
d:Foo("hi") -- 组内选 Bar(string)
d:print(1) -- 组内选 LogA(int)
-- d:Bar("x") -- 不可用

Static methods live on the type table: Demo.add_i32(...); instance methods use colon: obj:run_i32(...).

XML configuration

Separate from [LuaMarshalAs]:

AliasMarshalAs
Settings fieldluaAliasXmlPathsmarshalAsXmlPaths
Root elementZLuaAliasZLuaMarshalAs
FilesSeparate filesSeparate files
<?xml version="1.0" encoding="utf-8"?>
<ZLuaAlias version="1">
<Assembly name="Assembly-CSharp">
<Type fullName="Demo">
<Method name="Run" signature="(System.Int32)" alias="run_i32"/>
</Type>
<Type fullName="MyGame.UI.Panel">
<Method name="Show" signature="()" alias="show_panel"/>
</Type>
</Assembly>
</ZLuaAlias>
AttributeNotes
Assembly/@nameAssembly short name (e.g. Assembly-CSharp)
Type/@fullNameCLR full name; nested Outer+Inner
Method/@nameC# method name
Method/@signatureParameters only: () / (System.Int32); byref add &; no return type
Method/@aliasRequired; the method’s sole final Lua name

This file only allows AssemblyTypeMethod with @alias. Do not write MarshalAs / Param / Return, etc.

Precedence and platforms

  • Same method: Attribute > XML (Attribute present → ignore XML for that slot)
  • Mono: reads luaAliasXmlPaths at runtime
  • Il2Cpp: Generate writes static tables; Player does not read XML (re-Generate after XML changes)
  • Multiple @alias for the same (assembly, type, method, signature)fail

In Project Settings → ZLua, put XML paths in luaAliasXmlPaths (alongside the MarshalAs path list).

vs full-signature keys / register_method

ApproachNotes
Full-signature keyAutomatic at Bind; default name unchanged; demo['Run(System.Int32)'](demo, 5)
[LuaAlias] / XMLRename at Bind; preferred short colon names on hot paths
register_methodAt runtime, hang an existing direct onto a vacant short name; does not merge overloads

Common mistakes

SymptomCause
demo:Run(10) misses an aliased overloadAlready renamed; use the alias or another un-renamed overload
demo:Bar is nilBar was renamed by [LuaAlias("Foo")]
XML ignored (Player)Did not re-Generate; or path was put under marshalAsXmlPaths
Same root element as MarshalAsNeeds a separate ZLuaAlias file

Learning path

PreviousMethod overloads
NextExtension methods