private static void GenInterfaceCode(System.Type type, string name)
    {
        StringBuilder sb = new StringBuilder();

        s_Indent = 0;
        sb.AppendFormat("{0}using System;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections.Generic;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using GameFramework;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using GameFramework.Plugin;", GetIndentString());
        sb.AppendLine();
        sb.AppendLine();
        sb.AppendFormat("{0}namespace CsLibrary", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        sb.AppendFormat("{0}public interface {1}", GetIndentString(), name);
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        foreach (var prop in type.GetProperties(c_BindingFlags))
        {
            string typeName = SimpleName(prop.PropertyType);
            var    ps       = prop.GetIndexParameters();
            if (ps.Length > 0)
            {
                sb.AppendFormat("{0}{1} {2}[", GetIndentString(), typeName, prop.Name);
                OutputParams(sb, ps);
                sb.AppendLine("]");
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get;", GetIndentString());
                    sb.AppendLine();
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set;", GetIndentString());
                    sb.AppendLine();
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
            else
            {
                sb.AppendFormat("{0}{1} {2}", GetIndentString(), typeName, prop.Name);
                sb.AppendLine();
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get;", GetIndentString());
                    sb.AppendLine();
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set;", GetIndentString());
                    sb.AppendLine();
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
        }
        foreach (var method in type.GetMethods(c_BindingFlags))
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            string retType = SimpleName(method.ReturnType);
            if (retType == "System.Void")
            {
                retType = "void";
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            var  ps          = method.GetParameters();
            bool existParams = ExistParams(ps);
            sb.AppendFormat("{0}{1} {2}(", GetIndentString(), retType, method.Name);
            OutputParams(sb, ps);
            sb.AppendLine(");");
        }
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");

        File.WriteAllText(Path.Combine(c_InterfaceOutputDir, name + ".cs"), sb.ToString());

        Debug.Log(string.Format("GenInterfaceCode {0}.cs finish.", name));
    }
    private static void GenLuaProxyCode(System.Type type, string name)
    {
        StringBuilder sb = new StringBuilder();

        s_Indent = 0;
        sb.AppendFormat("{0}using System;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections.Generic;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using SLua;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using GameFramework;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using GameFramework.Plugin;", GetIndentString());
        sb.AppendLine();
        sb.AppendLine();
        bool useSpecClone = false;

        foreach (var method in type.GetMethods(c_BindingFlags))
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            if (method.Name == "Clone")
            {
                useSpecClone = type.Name.EndsWith("Plugin");
                break;
            }
        }
        if (useSpecClone || !type.IsInterface)
        {
            sb.AppendFormat("{0}public class {1} : LuaClassProxyBase", GetIndentString(), name);
        }
        else
        {
            sb.AppendFormat("{0}public class {1} : LuaClassProxyBase, {2}", GetIndentString(), name, type.Name);
        }
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        foreach (var prop in type.GetProperties(c_BindingFlags))
        {
            string typeName = SimpleName(prop.PropertyType);
            var    ps       = prop.GetIndexParameters();
            if (ps.Length > 0)
            {
                sb.AppendFormat("{0}public {1} {2}[", GetIndentString(), typeName, prop.Name);
                OutputParams(sb, ps);
                sb.AppendLine("]");
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}var err = LuaFunctionHelper.BeginCall(m_Cs2Lua_{1});", GetIndentString(), get.Name);
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(Self);", GetIndentString());
                    sb.AppendLine();
                    OutputArgs(sb, false, ps);
                    sb.AppendFormat("{0}var end_call_res = LuaFunctionHelper.EndCall(err);", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}if (end_call_res) {{", GetIndentString());
                    sb.AppendLine();
                    ++s_Indent;
                    sb.AppendFormat("{0}LuaFunctionHelper.BeginGetResult(err);", GetIndentString());
                    sb.AppendLine();
                    if (IsEnumType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}int __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    else if (IsValueType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}{1} __cs2lua_ret;", GetIndentString(), typeName);
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.AppendFormat("{0}object __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    sb.AppendFormat("{0}LuaFunctionHelper.EndGetResult();", GetIndentString());
                    sb.AppendLine();
                    if (IsEnumType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}return ({1})__cs2lua_ret;", GetIndentString(), typeName);
                        sb.AppendLine();
                    }
                    else if (IsValueType(prop.PropertyType) || typeName == "object")
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret as {1};", GetIndentString(), typeName);
                        sb.AppendLine();
                    }
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("} else {");
                    ++s_Indent;
                    OutputDefaultReturnValue(sb, prop.PropertyType, typeName);
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}var err = LuaFunctionHelper.BeginCall(m_Cs2Lua_{1});", GetIndentString(), set.Name);
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(Self);", GetIndentString());
                    sb.AppendLine();
                    OutputArgs(sb, false, ps);
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(value);", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.EndCallAndDiscardResult(err);", GetIndentString());
                    sb.AppendLine();
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
            else
            {
                sb.AppendFormat("{0}public {1} {2}", GetIndentString(), typeName, prop.Name);
                sb.AppendLine();
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}var err = LuaFunctionHelper.BeginCall(m_Cs2Lua_{1});", GetIndentString(), get.Name);
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(Self);", GetIndentString());
                    sb.AppendLine();
                    OutputArgs(sb, false, ps);
                    sb.AppendFormat("{0}var end_call_res = LuaFunctionHelper.EndCall(err);", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}if (end_call_res) {{", GetIndentString());
                    sb.AppendLine();
                    ++s_Indent;
                    sb.AppendFormat("{0}LuaFunctionHelper.BeginGetResult(err);", GetIndentString());
                    sb.AppendLine();
                    if (IsEnumType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}int __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    else if (IsValueType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}{1} __cs2lua_ret;", GetIndentString(), typeName);
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.AppendFormat("{0}object __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                        sb.AppendFormat("{0}LuaFunctionHelper.GetResult(out __cs2lua_ret);", GetIndentString());
                        sb.AppendLine();
                    }
                    sb.AppendFormat("{0}LuaFunctionHelper.EndGetResult();", GetIndentString());
                    sb.AppendLine();
                    if (IsEnumType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}return ({1})__cs2lua_ret;", GetIndentString(), typeName);
                        sb.AppendLine();
                    }
                    else if (IsValueType(prop.PropertyType) || typeName == "object")
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret;", GetIndentString());
                        sb.AppendLine();
                    }
                    else
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret as {1};", GetIndentString(), typeName);
                        sb.AppendLine();
                    }
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("} else {");
                    ++s_Indent;
                    OutputDefaultReturnValue(sb, prop.PropertyType, typeName);
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}var err = LuaFunctionHelper.BeginCall(m_Cs2Lua_{1});", GetIndentString(), set.Name);
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(Self);", GetIndentString());
                    sb.AppendLine();
                    OutputArgs(sb, false, ps);
                    sb.AppendFormat("{0}LuaFunctionHelper.PushValue(value);", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}LuaFunctionHelper.EndCallAndDiscardResult(err);", GetIndentString());
                    sb.AppendLine();
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
        }
        foreach (var method in type.GetMethods(c_BindingFlags))
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            string retType = SimpleName(method.ReturnType);
            if (retType == "System.Void")
            {
                retType = "void";
            }
            else if (retType == "System.Object")
            {
                retType = "object";
            }
            if (useSpecClone && method.Name == "Clone")
            {
                retType = "object";
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            var  ps          = method.GetParameters();
            bool existParams = ExistParams(ps);
            sb.AppendFormat("{0}public {1} {2}(", GetIndentString(), retType, method.Name);
            OutputParams(sb, ps);
            sb.AppendLine(")");
            sb.AppendFormat("{0}", GetIndentString());
            sb.AppendLine("{");
            ++s_Indent;
            sb.AppendFormat("{0}var err = LuaFunctionHelper.BeginCall(m_Cs2Lua_{1});", GetIndentString(), mi.MethodName);
            sb.AppendLine();
            sb.AppendFormat("{0}LuaFunctionHelper.PushValue(Self);", GetIndentString());
            sb.AppendLine();
            OutputArgs(sb, existParams, ps);
            sb.AppendFormat("{0}var end_call_res = LuaFunctionHelper.EndCall(err);", GetIndentString());
            sb.AppendLine();
            OutputMethodCallResult(sb, method, retType, ps);
            --s_Indent;
            sb.AppendFormat("{0}", GetIndentString());
            sb.AppendLine("}");
        }
        sb.AppendLine();
        sb.AppendFormat("{0}protected override void PrepareMembers()", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        foreach (var prop in type.GetProperties(c_BindingFlags))
        {
            var get = prop.GetGetMethod();
            var set = prop.GetSetMethod();
            if (null != get)
            {
                sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), get.Name, get.Name);
                sb.AppendLine();
            }
            if (null != set)
            {
                sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), set.Name, set.Name);
                sb.AppendLine();
            }
        }
        foreach (var method in type.GetMethods(c_BindingFlags))
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), mi.MethodName, mi.MethodName);
            sb.AppendLine();
        }
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");
        sb.AppendLine();
        foreach (var prop in type.GetProperties(c_BindingFlags))
        {
            var get = prop.GetGetMethod();
            var set = prop.GetSetMethod();
            if (null != get)
            {
                sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), get.Name);
                sb.AppendLine();
            }
            if (null != set)
            {
                sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), set.Name);
                sb.AppendLine();
            }
        }
        foreach (var method in type.GetMethods(c_BindingFlags))
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), mi.MethodName);
            sb.AppendLine();
        }
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");

        File.WriteAllText(Path.Combine(c_LuaProxyOutputDir, name + ".cs"), sb.ToString());

        Debug.Log(string.Format("GenLuaProxyCode {0}.cs finish.", name));
    }
Example #3
0
    private static void GenCode(System.Type type, string name)
    {
        StringBuilder sb = new StringBuilder();

        s_Indent = 0;
        sb.AppendFormat("{0}using System;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using System.Collections.Generic;", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}using SLua;", GetIndentString());
        sb.AppendLine();
        sb.AppendLine();
        sb.AppendFormat("{0}public class {1} : LuaClassProxyBase, {2}", GetIndentString(), name, type.Name);
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        foreach (var prop in type.GetProperties())
        {
            string typeName = SimpleName(prop.PropertyType);
            var    ps       = prop.GetIndexParameters();
            if (ps.Length > 0)
            {
                sb.AppendFormat("{0}public {1} {2}[", GetIndentString(), typeName, prop.Name);
                OutputParams(sb, ps);
                sb.AppendLine("]");
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    if (IsValueType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}return base.CastTo<{1}>(base.CallFunction(m_Cs2Lua_{2}, false, Self, ", GetIndentString(), typeName, get.Name);
                        OutputArgs(sb, ps);
                        sb.AppendLine("));");
                    }
                    else
                    {
                        sb.AppendFormat("{0}return base.CallFunction(m_Cs2Lua_{1}, false, Self, ", GetIndentString(), get.Name);
                        OutputArgs(sb, ps);
                        sb.AppendFormat(") as {0};", typeName);
                        sb.AppendLine();
                    }
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}base.CallFunction(m_Cs2Lua_{1}, false, Self, ", GetIndentString(), set.Name);
                    OutputArgs(sb, ps);
                    sb.AppendLine(", value);");
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
            else
            {
                sb.AppendFormat("{0}public {1} {2}", GetIndentString(), typeName, prop.Name);
                sb.AppendLine();
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("{");
                ++s_Indent;
                var get = prop.GetGetMethod();
                if (null != get)
                {
                    sb.AppendFormat("{0}get", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    if (IsValueType(prop.PropertyType))
                    {
                        sb.AppendFormat("{0}return base.CastTo<{1}>(base.CallFunction(m_Cs2Lua_{2}, false, Self));", GetIndentString(), typeName, get.Name);
                    }
                    else
                    {
                        sb.AppendFormat("{0}return base.CallFunction(m_Cs2Lua_{1}, false, Self) as {2};", GetIndentString(), get.Name, typeName);
                    }
                    sb.AppendLine();
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                var set = prop.GetSetMethod();
                if (null != set)
                {
                    sb.AppendFormat("{0}set", GetIndentString());
                    sb.AppendLine();
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("{");
                    ++s_Indent;
                    sb.AppendFormat("{0}base.CallFunction(m_Cs2Lua_{1}, false, Self, value);", GetIndentString(), set.Name);
                    sb.AppendLine();
                    --s_Indent;
                    sb.AppendFormat("{0}", GetIndentString());
                    sb.AppendLine("}");
                }
                --s_Indent;
                sb.AppendFormat("{0}", GetIndentString());
                sb.AppendLine("}");
            }
        }
        foreach (var method in type.GetMethods())
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            string retType = SimpleName(method.ReturnType);
            if (retType == "System.Void")
            {
                retType = "void";
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            var  ps          = method.GetParameters();
            bool existParams = ExistParams(ps);
            sb.AppendFormat("{0}public {1} {2}(", GetIndentString(), retType, method.Name);
            OutputParams(sb, ps);
            sb.AppendLine(")");
            sb.AppendFormat("{0}", GetIndentString());
            sb.AppendLine("{");
            ++s_Indent;
            OutputPreMethodCall(sb, method.GetParameters());
            sb.AppendFormat("{0}", GetIndentString());
            if (retType != "void" || mi.ExistReturnParam)
            {
                sb.Append("var __cs2lua_ret = ");
            }
            sb.AppendFormat("base.CallFunction(m_Cs2Lua_{0}, {1}, Self", mi.MethodName, existParams ? "true" : "false");
            if (mi.ExistParam)
            {
                sb.Append(", ");
            }
            OutputArgs(sb, ps);
            sb.Append(")");
            if (mi.ExistReturnParam)
            {
                sb.Append(" as object[]");
            }
            sb.AppendLine(";");
            OutputPostMethodCall(sb, retType, ps);
            if (retType != "void")
            {
                if (IsValueType(method.ReturnType))
                {
                    if (mi.ExistReturnParam)
                    {
                        sb.AppendFormat("{0}return base.CastTo<{1}>(__cs2lua_ret[0]);", GetIndentString(), retType);
                    }
                    else
                    {
                        sb.AppendFormat("{0}return base.CastTo<{1}>(__cs2lua_ret);", GetIndentString(), retType);
                    }
                }
                else
                {
                    if (mi.ExistReturnParam)
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret[0] as {1};", GetIndentString(), retType);
                    }
                    else
                    {
                        sb.AppendFormat("{0}return __cs2lua_ret as {1};", GetIndentString(), retType);
                    }
                }
                sb.AppendLine();
            }
            --s_Indent;
            sb.AppendFormat("{0}", GetIndentString());
            sb.AppendLine("}");
        }
        sb.AppendLine();
        sb.AppendFormat("{0}protected override void PrepareMembers()", GetIndentString());
        sb.AppendLine();
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("{");
        ++s_Indent;
        foreach (var prop in type.GetProperties())
        {
            var get = prop.GetGetMethod();
            var set = prop.GetSetMethod();
            if (null != get)
            {
                sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), get.Name, get.Name);
                sb.AppendLine();
            }
            if (null != set)
            {
                sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), set.Name, set.Name);
                sb.AppendLine();
            }
        }
        foreach (var method in type.GetMethods())
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            sb.AppendFormat("{0}m_Cs2Lua_{1} = (LuaFunction)Self[\"{2}\"];", GetIndentString(), mi.MethodName, mi.MethodName);
            sb.AppendLine();
        }
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");
        sb.AppendLine();
        foreach (var prop in type.GetProperties())
        {
            var get = prop.GetGetMethod();
            var set = prop.GetSetMethod();
            if (null != get)
            {
                sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), get.Name);
                sb.AppendLine();
            }
            if (null != set)
            {
                sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), set.Name);
                sb.AppendLine();
            }
        }
        foreach (var method in type.GetMethods())
        {
            if (method.IsSpecialName)
            {
                continue;
            }
            Cs2LuaMethodInfo mi = new Cs2LuaMethodInfo();
            mi.Init(method, type);
            sb.AppendFormat("{0}private LuaFunction m_Cs2Lua_{1};", GetIndentString(), mi.MethodName);
            sb.AppendLine();
        }
        --s_Indent;
        sb.AppendFormat("{0}", GetIndentString());
        sb.AppendLine("}");

        File.WriteAllText(Path.Combine(c_OutputDir, name + ".cs"), sb.ToString());

        Debug.Log(string.Format("GenCode {0}.cs finish.", name));
    }