LuaAlias
Provides a Lua-side rename key for C# method overloads, binding O(1) to a single overload and bypassing runtime dispatch. When an alias exists, the C# default method name is not registered.
using ZLua;
public class Demo
{
[LuaAlias("run_i32")]
public void Run(int value) { x = value; }
public void Run(string value) { x = value?.Length ?? 0; }
}
local demo = CSharp.AC.Demo()
demo:run_i32(10) -- hits Run(int) directly
demo:Run("hi") -- still goes through Run dispatch
Attribute Definition
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class LuaAliasAttribute : Attribute
{
public string Alias { get; }
public LuaAliasAttribute(string alias);
}
| Item | Description |
|---|---|
| Target | Method only |
| Multiplicity | At most one alias per method |
| Inheritance | Not inherited by subclass overrides |
Key-Space Rules
At Bind time, [LuaAlias] replaces that method's default Lua key; it allows collision with other methods' default names or aliases (collisions join the same overload group and use dispatch). If the alias has only one candidate, it is direct — preferred on hot paths.
Full rules: overload spec §5. When multiple same-name candidates exist, full-signature keys are also hung automatically (§3.7).
[LuaAlias("run_i32")] // usually single candidate → direct
public void Run(int value) { }
public void Run(string value) { } // default name still joins the "Run" group
XML Config (When Source Cannot Change)
Configured separately from [LuaMarshalAs]: Settings use the independent field luaAliasXmlPaths, root element ZLuaAlias.
<?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>
</Assembly>
</ZLuaAlias>
| Field | Description |
|---|---|
Assembly/@name | Assembly short name |
Type/@fullName | Full type name |
Method/@name | C# method name |
Method/@signature | Parameters only, e.g. (System.Int32) |
Method/@alias | Final Lua key (required; replaces default name) |
Priority: Attribute > XML. With an alias, MethodInfo.Name is not hung. Authoritative text: overload spec §5.4.
Static / Instance
| Method kind | Where alias is written | Lua call |
|---|---|---|
| Instance | Instance methodTable | obj:alias(...) |
| Static | Type-table methodTable | TypeTable.alias(...) |
Relation to Full-Signature Keys / register_method
| Approach | Timing | When to use |
|---|---|---|
Full-signature key Run(System.Int32) | Automatic at Bind (same-name multi-candidate) | Precise pick; no C# change, no API |
[LuaAlias] / XML | On type EnsureBinding | Compile-time short name; hot-path preferred |
zlua.register_method | Lua runtime | Hang a direct (often from a full-signature key) under a custom short name for obj:alias(...) |
[LuaAlias] / XML is a rename on methodTable (replaces the default key); full-signature keys pick which overload; register_method hangs an extra short name at runtime.
Mono / Il2Cpp Support
| Runtime | Support |
|---|---|
| Mono (Editor) | ✅ |
| Il2Cpp (Player) | ✅ |