Beispiel #1
0
        public static CompilationUnitSyntax Generate(Type t)
        {
            var cu = CompilationUnit();

            cu = cu.AddUsings(
                UsingDirective(IdentifierName("System")),
                UsingDirective(QualifiedName(QualifiedName(IdentifierName("System"), IdentifierName("IO")), IdentifierName("Pipelines"))),
                UsingDirective(QualifiedName(QualifiedName(IdentifierName("System"), IdentifierName("Text")), IdentifierName("Formatting"))),
                UsingDirective(QualifiedName(IdentifierName("System"), IdentifierName("Text"))));

            var c = ClassDeclaration("Serializer").WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.PartialKeyword)));
            var b = Block();

            b = WriteDeSerializer(t, b, IdentifierName("t"));

            var sc = new StringCollector();

            c = c.AddMembers(SpanHelpers.SpanField("span", sc.GetRawString()));

            foreach (var s in sc.strings)
            {
                var i = sc.GetOffset(s);
                c = c.AddMembers(SpanHelpers.SpanField("slice" + i.Item1, SpanHelpers.SpanSlice("span", i.Item1, i.Item2)));
            }

            c  = c.AddMembers(SerializerMethod(b, t));
            cu = cu.AddMembers(c);

            return(cu);
        }
        public static BlockSyntax ConvertToSlices(BlockSyntax b, StringCollector sc)
        {
            var cs  = b.ChildNodes().ToList();
            var res = Block();

            foreach (var cc in cs)
            {
                if (cc is IfStatementSyntax f && f.Statement is BlockSyntax block)
                {
                    if (f.Else != null)
                    {
                        res = res.AddStatements(IfStatement(f.Condition, ConvertToSlices(block, sc), ElseClause(ConvertToSlices((BlockSyntax)f.Else.Statement, sc))));
                    }
                    else
                    {
                        res = res.AddStatements(IfStatement(f.Condition, ConvertToSlices(block, sc)));
                    }
                }