예제 #1
0
        public static int CreateClass(IntPtr ptr)
        {
            try
            {
                string cls;
                CheckType(ptr, 1, out cls);
                Type t = LuaObject.FindType(cls);
                if (t == null)
                {
                    return(Error(ptr, string.Format("Can't find {0} to create", cls)));
                }

                ConstructorInfo[] cis    = t.GetConstructors();
                ConstructorInfo   target = null;
                for (int n = 0; n < cis.Length; n++)
                {
                    ConstructorInfo ci = cis[n];
                    if (LuaObject.MatchType(ptr, LuaNativeMethods.lua_gettop(ptr), 2, ci.GetParameters()))
                    {
                        target = ci;
                        break;
                    }
                }

                if (target != null)
                {
                    ParameterInfo[] pis  = target.GetParameters();
                    object[]        args = new object[pis.Length];
                    for (int n = 0; n < pis.Length; n++)
                    {
                        args[n] = LuaObject.ChangeType(LuaObject.CheckVar(ptr, n + 2), pis[n].ParameterType);
                    }

                    object ret = target.Invoke(args);
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushVar(ptr, ret);
                    return(2);
                }

                LuaObject.PushValue(ptr, true);
                return(1);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }