Esempio n. 1
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)));
        }
        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))));
        }
        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));
        }
Esempio n. 4
0
        protected override ImmutableArray <MemberDeclarationSyntax> GetMembers(SourceCodeSerializer serializer, ExpressionSyntax node, Type type)
        {
            var constructor = ConstructorDeclaration(ClassName)
                              .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword)))
                              .WithBody(Block(GetBody(serializer, node, type)));

            return(ImmutableArray.Create <MemberDeclarationSyntax>(constructor));
        }
Esempio n. 5
0
        protected virtual SeparatedSyntaxList <ExpressionStatementSyntax> GetBody(SourceCodeSerializer serializer, ExpressionSyntax node, Type type)
        {
            var obj = node as ObjectCreationExpressionSyntax;

            if (obj != null)
            {
                return(SeparatedList(obj.Initializer.Expressions.Select(ExpressionStatement)));
            }
            else
            {
                Debug.Fail($"Unknown kind: {node.Kind()}");

                return(SeparatedList(new[] { ExpressionStatement(node) }));
            }
        }
Esempio n. 6
0
        public sealed override SyntaxNode Generate(SourceCodeSerializer serializer, ExpressionSyntax node, Type type)
        {
            var usings  = serializer.Settings.Usings.Select(@using => UsingDirective(IdentifierName(@using)));
            var members = GetMembers(serializer, node, type);

            return(CompilationUnit()
                   .WithUsings(List(usings))
                   .WithMembers(
                       SingletonList <MemberDeclarationSyntax>(NamespaceDeclaration(IdentifierName(Namespace))
                                                               .WithMembers(
                                                                   SingletonList <MemberDeclarationSyntax>(
                                                                       ClassDeclaration(ClassName)
                                                                       .WithModifiers(TokenList(Token(SyntaxKind.PartialKeyword)))
                                                                       .WithMembers(List(members))
                                                                       )))));
        }
Esempio n. 7
0
 public abstract SyntaxNode Generate(SourceCodeSerializer serializer, ExpressionSyntax node, Type type);
 public override ExpressionSyntax ConvertToExpression(Type type, DateTime obj, SourceCodeSerializer serializer)
 {
     return(ParseExpression($"new DateTime({obj.Ticks})"));
 }
Esempio n. 9
0
 public override ExpressionSyntax ConvertToExpression(Type type, DateTimeOffset obj, SourceCodeSerializer serializer)
 {
     return(ParseExpression($"new DateTimeOffset({obj.Ticks}, new TimeSpan({obj.Offset.Ticks}))"));
 }
 public sealed override ExpressionSyntax ConvertToExpression(Type type, object obj, SourceCodeSerializer serializer) => ConvertToExpression(type, (T)obj, serializer);
 public abstract ExpressionSyntax ConvertToExpression(Type type, T obj, SourceCodeSerializer serializer);
Esempio n. 12
0
 public override ExpressionSyntax ConvertToExpression(Type type, object obj, SourceCodeSerializer serializer)
 {
     return(CornerCases(type, obj) ?? ParseExpression($"{obj}{GetSuffix(type)}"));
 }
Esempio n. 13
0
 public override ExpressionSyntax ConvertToExpression(Type type, decimal obj, SourceCodeSerializer serializer)
 {
     return(ParseExpression($"{obj}m"));
 }
Esempio n. 14
0
 protected abstract ImmutableArray <MemberDeclarationSyntax> GetMembers(SourceCodeSerializer serializer, ExpressionSyntax node, Type type);
        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))));
        }
 public override ExpressionSyntax ConvertToExpression(Type type, string obj, SourceCodeSerializer serializer)
 {
     return(ParseExpression($"@\"{obj}\""));
 }