How add meta functions to a C# class?

Mar 6, 2015 at 9:03 PM
I have a C# class:
namespace NeoTest1
    public class LuaMap
        private Dictionary<object,object> dict;
        public LuaMap(LuaTable table)
            dict = new Dictionary<object, object>(table.Values);

        public object this[object key]
                object obj;

                if (dict.TryGetValue(key, out obj))
                    return obj;
                return null;
                dict[key] = value;
I can access a map entry from Lua using "m['a']" which works fine.
map = clr.NeoTest1.LuaMap
local m = map{a=15,b=20,c=55} 
print(m['a'])  -- works
print(m.a)     -- does not work
I then tried a shortcut used on lua tables, "m.a", which does not work. How do I implement the "__index" meta function on a C# class?

If that is not possible, what are the alternatives that would support a syntax like 'm.a'?
Mar 6, 2015 at 9:42 PM
Edited Mar 6, 2015 at 9:43 PM
This is a long story.

There are three ways I have in mind for this question:
  1. Inherit from LuaTable and override OnIndex
  2. You inherit from DynamicObject (MSDN)
  3. You need to learn something about the DLR and IDynamicMetaObjectProvider (MSDN)
Hope this helps?
Mar 9, 2015 at 6:56 PM
Inheriting from LuaTable and overriding OnIndex and OnNewIndex worked for me. I also had to delete LuaMap's "public object this[object key]" operator because it conflicts with LuaTable's same operator.