static void GenInterfaceOrStructOrClass(Type type, TypeStatus ts,
                                                Func <Type, TypeStatus> getParent, Action <Type> onNewType)
        {
            TextFile tfFile = null;

            if (type.DeclaringType != null)
            {
                ts.IsInnerType = true;

                TypeStatus tsParent = getParent(type.DeclaringType);
                if (tsParent == null || tsParent.status == TypeStatus.Status.Wait)
                {
                    return;
                }

                if (tsParent.status == TypeStatus.Status.Ignored)
                {
                    ts.status = TypeStatus.Status.Ignored;
                    return;
                }

                tfFile = tsParent.tf.FindByTag("epos");
            }

            if (tfFile == null)
            {
                tfFile = new TextFile();
            }

            ts.tf     = tfFile;
            ts.status = TypeStatus.Status.Exported;

            GeneratorHelp.ATypeInfo ti = GeneratorHelp.CreateTypeInfo(type);

            StringBuilder sb   = new StringBuilder();
            TextFile      tfNs = tfFile;

            //string dir = Dir;
            if (type.DeclaringType == null &&
                !string.IsNullOrEmpty(type.Namespace))
            {
                tfNs = tfFile.Add("namespace {0}", type.Namespace).BraceIn();
                tfNs.BraceOut();
            }

            tfNs.Add("[Bridge.FileName(\"csw\")]");

            TextFile tfClass = null;

            sb.Remove(0, sb.Length);
            {
                if (type.IsPublic || type.IsNestedPublic)
                {
                    sb.Append("public ");
                }

                if (type.IsClass)
                {
                    if (type.IsAbstract && type.IsSealed)
                    {
                        sb.Append("static ");
                    }
                    //else if (type.IsAbstract)
                    //    sb.Append("abstract ");
                    //else if (type.IsSealed)
                    //    sb.Append("sealed ");

                    //if (type.is)
                }

                if (type.IsInterface)
                {
                    sb.Append("interface ");
                }
                else if (type.IsValueType)
                {
                    sb.Append("struct ");
                }
                else
                {
                    sb.Append("class ");
                }

                string className = type.CsFullName(CsNameOption.CompilableWithT);
                int    dot       = className.LastIndexOf(".");
                if (dot >= 0)
                {
                    className = className.Substring(dot + 1);
                }
                sb.Append(className);

                Type   vBaseType  = type.ValidBaseType();
                Type[] interfaces = type.GetDeclaringInterfaces();
                if (vBaseType != null || interfaces.Length > 0)
                {
                    sb.Append(" : ");

                    args a = new args();
                    if (vBaseType != null)
                    {
                        a.Add(typefn(vBaseType, type.Namespace));
                        onNewType(vBaseType);
                    }
                    foreach (var i in interfaces)
                    {
                        a.Add(typefn(i, type.Namespace));
                        onNewType(i);
                    }

                    sb.Append(a.ToString());
                }

                tfClass = tfNs.Add(sb.ToString()).BraceIn();
                tfClass.BraceOut();
            }

            tfClass.AddTag("epos");

            for (int i = 0; i < ti.Fields.Count; i++)
            {
                MemberInfoEx infoEx = ti.Fields[i];
                FieldInfo    field  = infoEx.member as FieldInfo;
                tfClass.Add("public {0}{1} {2};", (field.IsStatic ? "static " : ""), typefn(field.FieldType, type.Namespace), field.Name);

                onNewType(field.FieldType);
            }
            if (ti.Fields.Count > 0)
            {
                tfClass.AddLine();
            }

            for (int i = 0; i < ti.Cons.Count; i++)
            {
                MemberInfoEx    infoEx = ti.Cons[i];
                ConstructorInfo con    = infoEx.member as ConstructorInfo;

                if (type.IsValueType)
                {
                    // 结构体不需要无参数构造函数
                    if (con == null || con.GetParameters().Length == 0)
                    {
                        continue;
                    }
                }

                string ctorName = type.Name;
                if (type.IsGenericTypeDefinition)
                {
                    int flag = ctorName.LastIndexOf('`');
                    if (flag >= 0)
                    {
                        ctorName = ctorName.Substring(0, flag);
                    }
                }
                tfClass.Add("public extern {0}({1});", ctorName, con == null ? "" : Ps2String(type, con.GetParameters()));

                if (con != null)
                {
                    foreach (var p in con.GetParameters())
                    {
                        onNewType(p.ParameterType);
                    }
                }
            }
            if (ti.Cons.Count > 0)
            {
                tfClass.AddLine();
            }

            handlePros(tfClass, type, ti, onNewType);

            if (ti.Pros.Count > 0)
            {
                tfClass.AddLine();
            }

            handleMethods(tfClass, type, ti, onNewType);
            if (!type.IsInterface)
            {
                handleInterfaceProblems(tfClass, type, onNewType);
            }
        }
        static void GenInterfaceOrStructOrClass(Type type, TypeStatus ts,
                                                Func <Type, TypeStatus> getParent, Action <Type> onNewType)
        {
            TextFile tfFile = null;

            if (type.DeclaringType != null)
            {
                ts.IsInnerType = true;

                TypeStatus tsParent = getParent(type.DeclaringType);
                if (tsParent == null || tsParent.status == TypeStatus.Status.Wait)
                {
                    if (tsParent == null)
                    {
                        onNewType(type.DeclaringType);
                    }
                    return;
                }

                if (tsParent.status == TypeStatus.Status.Ignored)
                {
                    ts.status = TypeStatus.Status.Ignored;
                    return;
                }

                tfFile = tsParent.tf.FindByTag("epos");
            }

            if (tfFile == null)
            {
                tfFile = new TextFile();
            }

            ts.tf     = tfFile;
            ts.status = TypeStatus.Status.Exported;

            GeneratorHelp.ATypeInfo ti = GeneratorHelp.CreateTypeInfo(type);

            StringBuilder sb   = new StringBuilder();
            TextFile      tfNs = tfFile;

            //string dir = Dir;

            // // ignore Experimental @eugenejiang
            // if (type.Namespace != null && type.Namespace.IndexOf("UnityEngine.Experimental.") >= 0) {
            //  return;
            // }
            if (type.DeclaringType == null &&
                !string.IsNullOrEmpty(type.Namespace))
            {
                tfNs = tfFile.Add("namespace {0}", type.Namespace).BraceIn();
                tfNs.BraceOut();
            }

            GenAttributeForClassIfNeeded(type, tfNs);

            // multiple attributes

//            tfNs.Add("[Bridge.External]");
            TextFile tfClass = null;

            sb.Remove(0, sb.Length);
            {
                // force public
                if (type.IsPublic || type.IsNestedPublic || type.IsNested || type.IsNestedFamily)
                {
                    sb.Append("public ");
                }

                // if (type.IsNestedFamily) {
                //     sb.Append("protected ");
                // }

                // if (type.FullName.IndexOf("DropdownItem") >= 0) {
                //  Debug.Assert(false);
                // }

                if (type.IsClass)
                {
                    if (type.IsAbstract && type.IsSealed)
                    {
                        sb.Append("static ");
                    }
                    //else if (type.IsAbstract)
                    //    sb.Append("abstract ");
                    //else if (type.IsSealed)
                    //    sb.Append("sealed ");

                    //if (type.is)
                }

                if (type.IsInterface)
                {
                    sb.Append("interface ");
                }
                else if (type.IsValueType)
                {
                    sb.Append("struct ");
                }
                else
                {
                    sb.Append("class ");
                }

                string className = type.CsFullName(CsNameOption.CompilableWithT);
                int    dot       = className.LastIndexOf(".");
                if (dot >= 0)
                {
                    className = className.Substring(dot + 1);
                }
                sb.Append(className);

                Type   vBaseType  = type.ValidBaseType();
                Type[] interfaces = type.GetDeclaringInterfaces();
                if (vBaseType != null || interfaces.Length > 0)
                {
                    sb.Append(" : ");

                    args a = new args();
                    if (vBaseType != null)
                    {
                        a.Add(typefn(vBaseType, type.Namespace, CsNameOption.CompilableWithT));
                        onNewType(vBaseType);
                    }
                    foreach (var i in interfaces)
                    {
                        a.Add(typefn(i, type.Namespace, CsNameOption.CompilableWithT));
                        onNewType(i);
                    }

                    sb.Append(a.ToString());
                }

                tfClass = tfNs.Add(sb.ToString()).BraceIn();
                tfClass.BraceOut();
            }

            tfClass.AddTag("epos");

            if (handleEvents(tfClass, type, onNewType))
            {
                tfClass.AddLine();
            }

            for (int i = 0; i < ti.Fields.Count; i++)
            {
                MemberInfoEx infoEx            = ti.Fields[i];
                FieldInfo    field             = infoEx.member as FieldInfo;
                var          publicOrProtected = "public";
                if (field.IsFamily)
                {
                    // publicOrProtected = "protected";
                }
                if (field.IsLiteral && !field.IsInitOnly)
                {
                    {
                        var attributes = field.GetCustomAttributes();
                        foreach (var attr in attributes)
                        {
                            tfClass.Add("[{0}]", attr.GetType().Name);
                            onNewType(attr.GetType());
                        }
                    }



                    if ("string" == typefn(field.FieldType, type.Namespace))
                    {
                        tfClass.Add(publicOrProtected + " const {0} {1} = \"{2}\";", typefn(field.FieldType, type.Namespace), field.Name, field.GetValue(null));
                    }
                    else if ("float" == typefn(field.FieldType, type.Namespace))
                    {
                        var    fv = (float)field.GetValue(null);
                        string defaultvalue;
                        if (float.IsNaN(fv))
                        {
                            defaultvalue = "float.NaN";
                        }
                        else if (float.IsPositiveInfinity(fv))
                        {
                            defaultvalue = "float.PositiveInfinity";
                        }
                        else if (float.IsNegativeInfinity(fv))
                        {
                            defaultvalue = "float.NegativeInfinity";
                        }
                        else
                        {
                            defaultvalue = fv + "f";
                        }
                        tfClass.Add(publicOrProtected + " const {0} {1} = {2};", typefn(field.FieldType, type.Namespace), field.Name, defaultvalue);
                    }
                    else
                    {
                        tfClass.Add(publicOrProtected + " const {0} {1} = {2};", typefn(field.FieldType, type.Namespace), field.Name, field.GetValue(null));
                    }
                }
                else
                {
                    tfClass.Add(publicOrProtected + " {0}{1} {2};", (field.IsStatic ? "static " : ""), typefn(field.FieldType, type.Namespace), field.Name);
                }

                onNewType(field.FieldType);
            }
            if (ti.Fields.Count > 0)
            {
                tfClass.AddLine();
            }

            // Constructors
            for (int i = 0; i < ti.Cons.Count; i++)
            {
                MemberInfoEx    infoEx = ti.Cons[i];
                ConstructorInfo con    = infoEx.member as ConstructorInfo;

                if (type.IsValueType)
                {
                    // 结构体不需要无参数构造函数
                    if (con == null || con.GetParameters().Length == 0)
                    {
                        continue;
                    }
                }

                string ctorName = type.Name;
                if (type.IsGenericTypeDefinition)
                {
                    int flag = ctorName.LastIndexOf('`');
                    if (flag >= 0)
                    {
                        ctorName = ctorName.Substring(0, flag);
                    }
                }

                var constructorBuilder = new StringBuilder();
                var paras = con == null ? "" : Ps2String(type, con.GetParameters());
                constructorBuilder.Append($"public {ctorName}({paras})");
                var baseType = type.ValidBaseType();
                if (baseType != null && !HasDefaultConstructor(baseType))
                {
                    constructorBuilder.Append($" : base({BaseConstructorParameters(baseType)})");
                }
                constructorBuilder.Append(" { throw new Exception(\"not impl\"); }");

                tfClass.Add(constructorBuilder.ToString());

                if (con != null)
                {
                    foreach (var p in con.GetParameters())
                    {
                        onNewType(p.ParameterType);
                    }
                }
            }
            if (ti.Cons.Count > 0)
            {
                tfClass.AddLine();
            }

            handlePros(tfClass, type, ti, onNewType);

            if (ti.Pros.Count > 0)
            {
                tfClass.AddLine();
            }

            handleMethods(tfClass, type, ti, onNewType);
            if (!type.IsInterface)
            {
                handleInterfaceProblems(tfClass, type, onNewType);
            }
        }