Пример #1
0
        public override void Generate(HashSet <RootType> referTypes)
        {
            if (Type.IsEnum)
            {
                GenerateEnum(referTypes);
                return;
            }

            AddChild(Header = new TypeNode(Root), referTypes);

            List <FieldInfo> fields;
            var customInterface = Type.GetInterfaces().FirstOrDefault(t => t.Name.StartsWith("ICustomSerialization"));

            if (customInterface != null)
            {
                var arguments = customInterface.GetGenericArguments();
                CustomSerializeParams = new SerializeNode[arguments.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    AddChild(CustomSerializeParams[i] = SerializeNode.Create(arguments[i]), referTypes);
                }
                fields = new List <FieldInfo>();
            }
            else
            {
                fields = GetSerializeFields(Type);
            }

            AddChild(ReadPart  = new ReadPartNode(Root, fields), referTypes);
            AddChild(WritePart = new WritePartNode(Root, fields), referTypes);

            FileName = Header.SerializerTypeName;
        }
Пример #2
0
        protected void AppendWritePart(WritePartNode node, TextBuilder builder)
        {
            if (node.FirstChild is ArrayTypeNode)
            {
                VisitWriteArray(VARIABLE, STREAM, node.FirstChild as ArrayTypeNode, builder);
            }
            else if (node.FirstChild is ListTypeNode)
            {
                VisitWriteList(VARIABLE, STREAM, node.FirstChild as ListTypeNode, builder);
            }
            else if (node.FirstChild is DictionaryTypeNode)
            {
                VisitWriteDictionary(VARIABLE, STREAM, node.FirstChild as DictionaryTypeNode, builder);
            }
            else if (node.FirstChild is HashSetTypeNode)
            {
                VisitWriteHashSet(VARIABLE, STREAM, node.FirstChild as HashSetTypeNode, builder);
            }
            else if (node.FirstChild is PolyClassTypeNode)
            {
                VisitWritePolyClass(VARIABLE, STREAM, node.FirstChild as PolyClassTypeNode, builder);
            }
            else if (node.FirstChild is TwoDimArrayTypeNode)
            {
                VisitWriteTwoDimArray(VARIABLE, STREAM, node.FirstChild as TwoDimArrayTypeNode, builder);
            }
            else if (node.FirstChild is StringTypeNode)
            {
                VisitWriteString(VARIABLE, STREAM, builder);
            }
            else
            {
                foreach (var child in node.Children)
                {
                    if (child is FieldNode)
                    {
                        var field = child as FieldNode;
                        builder.Push();
                        VisitField(VARIABLE, field, builder);
                        var variable = builder.Pop();

                        VisitWriteField(variable, STREAM, field.Serialize, builder);
                    }
                }
            }
        }
Пример #3
0
        protected virtual void VisitWritePart(WritePartNode node, TextBuilder builder)
        {
            builder.WriteLine("void IConfigSerializer.Write(IBinaryWriter writer, object o)");
            builder.LeftPar();
            builder.Indent();
            builder.WriteLine("Write(writer, ({0})o);", node.TypeName);
            builder.UnIndent();
            builder.RightPar();

            builder.EmptyLine();

            builder.WriteLine("public static void Write(IBinaryWriter o, {0} d)", node.TypeName);
            builder.LeftPar();
            builder.Indent();

            if (!node.Type.IsValueType)
            {
                builder.WriteLine("o.Write(d != null);");
                builder.WriteLine("if(d == null) return;");
                builder.EmptyLine();
            }

            var root = node.Parent as RootNode;

            if (root != null && root.CustomSerializeParams != null)
            {
                AppendWriteFromCustom("o", root.CustomSerializeParams, builder);
            }
            else
            {
                AppendWritePart(node, builder);
            }

            builder.UnIndent();
            builder.RightPar();
        }