MetaTablesInCSharp - nrother/dynamiclua GitHub Wiki

Metatable usage in C#

Introduction

In Lua you can assign a Metatable to a table to specify the behaviour in unsupported cases. E.g. you can specify what happens, if you try to add to tables, try to access a non-existent field, etc. If this concept is new to you, you might have a look to the corresponding Lua Wiki Entry When using a DynamicLuaTable that represents a Lua table, that has an associated Metatable, you can use this Metafunctions in C# like you would expect. I.e. when you implement the __add-Metamethod, you can add these tables in C# just like numbers.

The following Metamethods are currently supported:

Metamethod C# code to use it Notes
__add, __sub, __mul, __div a + b, a - b, a * b, a / b
__mod a % b
__pow a.Power(b) Can be used from VB, etc natively with a^b
__eq a == b, a != b For "not equal" the result will be negated (if it's bool)
__lt, __le a < b, a <= b, a > b, a >= b For "bigger then" operations, the operands will be switched
__call a(b)
__index a.b, a[b]
__newindex a.b = c, a[b] = c
__unm -a
__tostring a.ToString()

The following Metamethods are currently not supported:

Metamethod Notes
__concat There is no special concat operator in C#, addition and concatenation is done with the plus sign (+)
__len There is no len operator in C#

Example 1

This in Example a Metamethod __index is created, mapping the square of each number to the corresponding table entry.

dynamic tab = lua.NewTable("tab");
dynamic mt = lua.NewTable("mt");
mt.__index = new Func<dynamic, string, double>((t, i) => Math.Pow(int.Parse(i), 2));
tab.SetMetatable(mt);

for (int i = 0; i <= 10; i++)
{
      Console.WriteLine(tab[i]); //Prints 0,1,4,9,etc.
}

The Metamethod is implemented as a lambada Function in C#. It takes two arguments, the table t and the index i. The table has the type dynamic (but is an instance if LuaTable, see issue #4), the index is a string. The return value is double, the CLR type for the Lua type number.

Example 2

In this example, a simple "class" is created (this time in Lua), both representing basically two numbers, and implementing a __add Method in their Metatable. With this setup you can add these two classes in C#.

lua("c1 = { num = 42 }; c2 = { num = 7 }");
lua("mtc = { __add = function(t, other) return t.num + other.num end }");
lua("setmetatable(c1, mtc)");
lua("setmetatable(c2, mtc)");

Console.WriteLine(lua.c1 + lua.c2); //Prints 49
⚠️ **GitHub.com Fallback** ⚠️