Skip to main content

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);
}
ItemDescription
TargetMethod only
MultiplicityAt most one alias per method
InheritanceNot 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>
FieldDescription
Assembly/@nameAssembly short name
Type/@fullNameFull type name
Method/@nameC# method name
Method/@signatureParameters only, e.g. (System.Int32)
Method/@aliasFinal 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 kindWhere alias is writtenLua call
InstanceInstance methodTableobj:alias(...)
StaticType-table methodTableTypeTable.alias(...)

Relation to Full-Signature Keys / register_method

ApproachTimingWhen to use
Full-signature key Run(System.Int32)Automatic at Bind (same-name multi-candidate)Precise pick; no C# change, no API
[LuaAlias] / XMLOn type EnsureBindingCompile-time short name; hot-path preferred
zlua.register_methodLua runtimeHang 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

RuntimeSupport
Mono (Editor)
Il2Cpp (Player)