public override ExpressionSyntax ConvertToExpression(Type type, object obj, SourceCodeSerializer serializer)
        {
            var properties = GetProperties(type);

            var propertyNodes = properties.Select(property =>
            {
                var value      = property.GetValue(obj);
                var expression = serializer.GetExpression(value);

                if (expression == null)
                {
                    return(null);
                }

                var assignment = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, IdentifierName(property.Name), expression);

                if (serializer.Settings.ObjectInitializationNewLine)
                {
                    return(assignment.WithLeadingTrivia(TriviaList(LineFeed)));
                }
                else
                {
                    return(assignment);
                }
            }).Where(prop => prop != null);

            return(ObjectCreationExpression(serializer.GetTypeName(obj.GetType()))
                   .WithInitializer(
                       InitializerExpression(SyntaxKind.ObjectInitializerExpression, SeparatedList <ExpressionSyntax>(propertyNodes))));
        }
Ejemplo n.º 2
0
        public override ExpressionSyntax ConvertToExpression(Type type, object obj, SourceCodeSerializer serializer)
        {
            var entries = GetFlags(type, (Enum)obj)
                          .Select(e => $"{serializer.GetTypeName(type)}.{e}");

            return(ParseExpression(string.Join(" | ", entries)));
        }
        protected override ImmutableArray <MemberDeclarationSyntax> GetMembers(SourceCodeSerializer serializer, ExpressionSyntax node, Type type)
        {
            var method = MethodDeclaration(serializer.GetTypeName(type), Identifier(MethodName))
                         .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                         .WithBody(Block(SingletonList <StatementSyntax>(ReturnStatement(node))));

            return(ImmutableArray.Create <MemberDeclarationSyntax>(method));
        }
        public override ExpressionSyntax ConvertToExpression(Type type, IEnumerable collection, SourceCodeSerializer serializer)
        {
            var arrayType = GetGenericParameter(type);
            var generic   = collection.Cast <object>();

            var items = generic.Select(item => serializer.GetExpression(item).WithLeadingTrivia(TriviaList(LineFeed)));

            return(ArrayCreationExpression(
                       ArrayType(serializer.GetTypeName(arrayType), SingletonList(ArrayRankSpecifier(SingletonSeparatedList <ExpressionSyntax>(OmittedArraySizeExpression())))))
                   .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression, SeparatedList(items))));
        }