Пример #1
0
        /// <summary>
        /// 不包含parse field
        /// </summary>
        internal void Parse()
        {
            Dependents.Clear();
            Summarys.Clear();
            Fields.Clear();
            List <List <Line> > fields = ParseFieldLine(ParseMsgElement());

            foreach (List <Line> list in fields)
            {
                ProtoBufferField field = new ProtoBufferField(this, list);
                Fields.Add(field);
            }
        }
        /// <summary>
        /// c# 写入ToString
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="message"></param>
        private void Write2StringCShape(StreamWriter writer, ProtoBufferMessage message)
        {
            writer.WriteLine(string.Format("{0}public override string ToString()", Tap2));
            writer.WriteLine(Tap2Left);
            writer.WriteLine(string.Format("{0}StringBuilder sb = new StringBuilder();", Tap3));
            writer.WriteLine(string.Format("{0}sb.Append", Tap3) + "(\"{\");");
            for (int i = 0; i < message.Fields.Count; i++)
            {
                ProtoBufferField field        = message.Fields[i];
                string           checkName    = CheckRequiredName(field.Name, field.RequiredType);
                string           propertyName = PropertyName(field.Name, field.RequiredType);
                if (i == message.Fields.Count - 1)
                {
                    switch (field.RequiredType)
                    {
                    case RequiredType.Required:
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : \" + {1});", Tap3, propertyName));
                        break;

                    case RequiredType.Optional:
                        writer.WriteLine(string.Format("{0}if({1})", Tap3, checkName));
                        writer.WriteLine(Tap3Left);
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : \" + {1});", Tap4, propertyName));
                        writer.WriteLine(Tap3Right);
                        break;

                    case RequiredType.Repeated:
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : [\");", Tap3, propertyName));

                        writer.WriteLine(string.Format("{0}for(int i = 0; i < {1}.Count;i ++)", Tap3, propertyName));
                        writer.WriteLine(Tap3Left);
                        writer.WriteLine(string.Format("{0}if(i == {1}.Count -1)", Tap4, propertyName));
                        writer.WriteLine(Tap4Left);
                        writer.WriteLine(string.Format("{0}sb.Append({1}[i]);", Tap5, propertyName));
                        writer.WriteLine(string.Format("{0}else", Tap4Right));
                        writer.WriteLine(Tap4Left);
                        writer.WriteLine(string.Format("{0}sb.Append({1}[i] + \",\");", Tap5, propertyName));
                        writer.WriteLine(Tap4Right);
                        writer.WriteLine(Tap3Right);
                        writer.WriteLine(string.Format("{0}sb.Append(\"]\");", Tap3));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    switch (field.RequiredType)
                    {
                    case RequiredType.Required:
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : \" + {1} + \",\");", Tap3, propertyName));
                        break;

                    case RequiredType.Optional:
                        writer.WriteLine(string.Format("{0}if({1})", Tap3, checkName));
                        writer.WriteLine(Tap3Left);
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : \" + {1} +\",\");", Tap4, propertyName));
                        writer.WriteLine(Tap3Right);
                        break;

                    case RequiredType.Repeated:
                        writer.WriteLine(string.Format("{0}sb.Append(\"{1} : [\");", Tap3, propertyName));

                        writer.WriteLine(string.Format("{0}for(int i = 0; i < {1}.Count;i ++)", Tap3, propertyName));
                        writer.WriteLine(Tap3Left);
                        writer.WriteLine(string.Format("{0}if(i == {1}.Count -1)", Tap4, propertyName));
                        writer.WriteLine(Tap4Left);
                        writer.WriteLine(string.Format("{0}sb.Append({1}[i]);", Tap5, propertyName));
                        writer.WriteLine(string.Format("{0}else", Tap4Right));
                        writer.WriteLine(Tap4Left);
                        writer.WriteLine(string.Format("{0}sb.Append({1}[i] + \",\");", Tap5, propertyName));
                        writer.WriteLine(Tap4Right);
                        writer.WriteLine(Tap3Right);
                        writer.WriteLine(string.Format("{0}sb.Append(\"],\");", Tap3));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
            writer.WriteLine(string.Format("{0}sb.Append", Tap3) + "(\"}\");");
            writer.WriteLine(string.Format("{0}return sb.ToString();", Tap3));
            writer.WriteLine(Tap2Right);
        }
Пример #3
0
        /// <summary>
        /// 不包含parse field
        /// </summary>
        internal void Parse()
        {
            Dependents.Clear();
            Summarys.Clear();
            Fields.Clear();
            List<List<Line>> fields = ParseFieldLine(ParseMsgElement());

            foreach (List<Line> list in fields)
            {
                ProtoBufferField field = new ProtoBufferField(this,list);
                Fields.Add(field);
            }
        }