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)); }
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)); }
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) })); } }
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)) ))))); }
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})")); }
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);
public override ExpressionSyntax ConvertToExpression(Type type, object obj, SourceCodeSerializer serializer) { return(CornerCases(type, obj) ?? ParseExpression($"{obj}{GetSuffix(type)}")); }
public override ExpressionSyntax ConvertToExpression(Type type, decimal obj, SourceCodeSerializer serializer) { return(ParseExpression($"{obj}m")); }
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}\"")); }