public static SyntaxNode ArrayCreationExpression(this SyntaxGenerator generator, SyntaxNode type, IEnumerable <SyntaxNode> sizeExpression)
        {
            if (generator.NullLiteralExpression() is CS.ExpressionSyntax)
            {
                CS.TypeSyntax typeSyntax = type as CS.TypeSyntax;
                if (typeSyntax == null)
                {
                    throw new ArgumentException($"Invalid syntax node type; Expected {typeof(CS.TypeSyntax).FullName}.", "type");
                }

                IEnumerable <CS.ExpressionSyntax> csSizeExpressions = sizeExpression.Select(exp => exp as CS.ExpressionSyntax);
                if (csSizeExpressions.Any(exp => exp == null))
                {
                    throw new ArgumentException($"Invalid syntax node type; Expected {typeof(CS.ExpressionSyntax).FullName}.", "sizeExpression");
                }

                return(CSSF.ArrayCreationExpression(
                           CSSF.ArrayType(
                               typeSyntax,
                               CSSF.SingletonList(
                                   CSSF.ArrayRankSpecifier(
                                       CSSF.SeparatedList(csSizeExpressions)
                                       )
                                   )
                               )
                           ));
            }
            else
            {
                throw new ArgumentException("Not a CSharp ExpressionSyntax");
            }
        }
Пример #2
0
        public TypeSyntax GetTypeSyntax(TypeReference typeReference)
        {
            bool isOptional = (typeReference ?? throw new ArgumentNullException(nameof(typeReference))).IsOptional == true;

            if (typeReference.Primitive != null)
            {
                switch (typeReference.Primitive.Value)
                {
                case PrimitiveType.Any:
                    return(SF.ParseTypeName("object"));

                case PrimitiveType.Boolean:
                    return(SF.ParseTypeName(isOptional ? "bool?" : "bool"));

                case PrimitiveType.Date:
                    return(SF.ParseTypeName(isOptional ? "DateTime?" : "DateTime"));

                case PrimitiveType.Json:
                    return(SF.ParseTypeName("JObject"));

                case PrimitiveType.Number:
                    return(SF.ParseTypeName(isOptional ? "double?" : "double"));

                case PrimitiveType.String:
                    return(SF.ParseTypeName("string"));

                default:
                    throw new ArgumentException($"Unexpected primitive type {typeReference.Primitive.Value}", nameof(typeReference));
                }
            }

            if (typeReference.Collection != null)
            {
                TypeSyntax elementType = GetTypeSyntax(typeReference.Collection.ElementType);

                switch (typeReference.Collection.Kind)
                {
                case CollectionKind.Array:
                    return(SF.ArrayType(
                               elementType,
                               SF.List(new[] { SF.ArrayRankSpecifier() })
                               ));

                case CollectionKind.Map:
                    return(SF.ParseTypeName($"IDictionary<string, {elementType}>"));

                default:
                    throw new ArgumentException($"Unexpected collection type {typeReference.Collection.Kind}", nameof(typeReference));
                }
            }

            if (typeReference.Union != null)
            {
                return(SF.ParseTypeName("object"));
            }

            if (typeReference.FullyQualifiedName != null)
            {
                Type type = GetTypeFromFullyQualifiedName(typeReference.FullyQualifiedName);

                return(SF.ParseTypeName(GetName(type)));
            }

            throw new ArgumentException("Invalid type reference", nameof(typeReference));
        }