コード例 #1
0
        static int GetMethod(IntPtr L)
        {
            try
            {
                int    count = LuaDLL.lua_gettop(L);
                Type   t     = ToLua.CheckMonoType(L, 1);
                string name  = ToLua.CheckString(L, 2);
                Type[] types = null;

                if (count > 2)
                {
                    types = new Type[count - 2];

                    for (int i = 3; i <= count; i++)
                    {
                        Type ti = ToLua.CheckMonoType(L, i);
                        if (ti == null)
                        {
                            LuaDLL.luaL_typerror(L, i, "Type");
                        }
                        types[i - 3] = ti;
                    }
                }

                MethodInfo md = null;

                // 通过签名获取方法
                if (types == null)
                {
                    md = t.GetMethod(name);
                }
                else
                {
                    md = t.GetMethod(name, types);
                }

                PushLuaMethod(L, md, t, types);
            }
            catch (Exception e)
            {
                return(LuaDLL.toluaL_exception(L, e));
            }

            return(1);
        }
コード例 #2
0
        static int CreateInstance(IntPtr L)
        {
            try
            {
                Type t = ToLua.CheckMonoType(L, 1);
                if (t == null)
                {
                    LuaDLL.luaL_typerror(L, 1, "Type");
                }
                int    count = LuaDLL.lua_gettop(L);
                object obj   = null;

                if (count == 1)
                {
                    obj = Activator.CreateInstance(t);
                }
                else
                {
                    object[] args = new object[count - 1];

                    for (int i = 2; i <= count; i++)
                    {
                        args[i - 2] = ToLua.ToVarObject(L, i);
                    }

                    obj = Activator.CreateInstance(t, args);
                }

                ToLua.Push(L, obj);
            }
            catch (Exception e)
            {
                return(LuaDLL.toluaL_exception(L, e));
            }

            return(1);
        }