コード例 #1
0
        private static SyntaxNode FixCtorComment(ConstructorDeclarationSyntax ctor)
        {
            var typeDeclarationSyntax = ctor.Ancestors().OfType <TypeDeclarationSyntax>().First();
            var type = SyntaxFactory.ParseTypeName(typeDeclarationSyntax.Identifier.ValueText);

            // we have a ctor
            var parameters = ctor.ParameterList.Parameters;

            switch (parameters.Count)
            {
            case 0:
                return(FixParameterlessCtor(type));

            case 1 when parameters[0].Type.IsString():
コード例 #2
0
            public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
            {
                if (!node.AttributeLists.Any())
                {
                    base.VisitConstructorDeclaration(node);
                    return;
                }

                var hasAttribute = node.AttributeLists
                                   .SelectMany(al => al.Attributes)
                                   .Any(attr => attr.Name.ToString() == "JsonConstructor");

                if (!hasAttribute)
                {
                    base.VisitConstructorDeclaration(node);
                    return;
                }

                var semanticModel = Compilation.GetSemanticModel(node.SyntaxTree);

                var typeName = node
                               .Ancestors().OfType <TypeDeclarationSyntax>().First()
                               .ToTypeName(semanticModel);

                var converterName = $"{typeName.ShortName}ConstructorConverter";

                JsonTypes.Add(typeName, new JsonType(
                                  type: typeName,
                                  directReadExpression: jsonTypes => $"{converterName}.ReadInternal(ref reader, options)",
                                  directWriteStatement: (jsonTypes, valueExpr) => $"{converterName}.WriteInternal(writer, {valueExpr}, options);",
                                  converterName: converterName,
                                  constructionMember: node,
                                  outputMember: null,
                                  tinyType: null
                                  ));
            }