示例#1
0
        private static Class BuildBaseClass(Record record, Dictionary<string, Record> AllRecords)
        {
            string className = record.Name;
            Class baseClass = new Class(className);
            baseClass.Modifiers.Add("public");
            baseClass.Modifiers.Add("partial");
            if (record.Parent != null)
            {
                baseClass.BaseClass = record.Parent;
                baseClass.Constructors.Add(String.Format("public {0}() {{ }}", className));
                baseClass.Constructors.Add(String.Format(
                    "public {0}({1} record) : base(record) {{ }}",
                    className, baseClass.BaseClass
                    ));
                if (record.Fields.Count > 0)
                {
                    foreach (RecordField member in record.Fields)
                    {
                        Field field = new Field(member.Type, member.Name);
                        field.Modifiers.Add("public");
                        if (member.Description != null)
                        {
                            field.Summary = member.Description;
                        }
                        baseClass.Members.Add(field);
                    }
                }
            }
            else
            {
                Method method = new Method(className, "Read");
                method.Modifiers.Add("public");
                method.Modifiers.Add("static");
                method.Params.Add(new Parameter("Stream", "stream"));

                method.MethodBody.Add(String.Format("{0} record = {0}.ReadBase(stream);", className));
                CodeBlock switchblock = new CodeBlock("switch (record.Type)");
                AddChildCase(record, className, switchblock);
                CodeLines defaultblock = new CodeLines();
                defaultblock.Lines.Add("default:");
                defaultblock.Lines.Add("\treturn record;");
                switchblock.Add(defaultblock);
                method.MethodBody.Add(switchblock);

                baseClass.Members.Add(method);
            }
            return baseClass;
        }
 private static void AddChildCase(Record record, string className, CodeBlock switchblock)
 {
     foreach (Record childRecord in record.ChildRecords)
     {
         if (!childRecord.IsAbstract)
         {
             string name = childRecord.Name;
             CodeLines caseblock = new CodeLines();
             caseblock.Lines.Add(String.Format("case {0}Type.{1}:", className, name));
             caseblock.Lines.Add(String.Format("\treturn new {0}(record);", name));
             switchblock.Add(caseblock);
         }
         else
         {
             AddChildCase(childRecord, className, switchblock);
         }
     }
 }