/// <summary> /// Creates a closed generic type from the a generic type definition (like <see cref="IEnumerable{T}"/>) and type arguments (like <see cref="string"/>). /// </summary> /// <param name="genericTypeDefinition">The generic type definition</param> /// <param name="typeArguments">Type arguments</param> /// <returns>The generic type.</returns> public static TypeSyntax CreateGenericTypeFromGenericTypeDefinition(TypeSyntax genericTypeDefinition, params TypeSyntax[] typeArguments) { GenericNameSyntax openType = genericTypeDefinition.DescendantNodes().OfType <GenericNameSyntax>().Single(); GenericNameSyntax closedType = openType.AddTypeArgumentListArguments(typeArguments); return(genericTypeDefinition.ReplaceNode(openType, closedType)); }
public CSharpSyntaxNode Convert(AsExpression node) { if (TypeHelper.IsArrayType(node.Type)) //to .AsArray<T>() { GenericNameSyntax csName = SyntaxFactory.GenericName("AsArray"); if (node.Type.Kind == NodeKind.ArrayType) { csName = csName.AddTypeArgumentListArguments((node.Type as ArrayType).ElementType.ToCsNode <TypeSyntax>()); } else if (node.Type.Kind == NodeKind.TypeReference) { csName = csName.AddTypeArgumentListArguments((node.Type as TypeReference).TypeArguments[0].ToCsNode <TypeSyntax>()); } return(SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, node.Expression.ToCsNode <ExpressionSyntax>(), csName)) .AddArgumentListArguments()); } else if (TypeHelper.ToShortName(node.Type.Text) == "DataValueType") { GenericNameSyntax csName = SyntaxFactory.GenericName("As"); csName = csName.AddTypeArgumentListArguments(node.Type.ToCsNode <TypeSyntax>()); return(SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, node.Expression.ToCsNode <ExpressionSyntax>(), csName)) .AddArgumentListArguments()); } else { return(SyntaxFactory.BinaryExpression( SyntaxKind.AsExpression, node.Expression.ToCsNode <ExpressionSyntax>(), node.Type.ToCsNode <ExpressionSyntax>())); } }
public CSharpSyntaxNode Convert(TupleType node) // let a: [number, string] = [1, "hello"]; { GenericNameSyntax generic = this.Context.Config.PreferTypeScriptType ? SyntaxFactory.GenericName("Array") : SyntaxFactory.GenericName("List"); Node elementType = this.GetElementType(node); if (elementType != null) { generic = generic.AddTypeArgumentListArguments(elementType.ToCsNode <TypeSyntax>()); } else { generic = this.Context.Config.PreferTypeScriptType ? generic.AddTypeArgumentListArguments(SyntaxFactory.IdentifierName("Object")) : generic.AddTypeArgumentListArguments(SyntaxFactory.IdentifierName("object")); } return(generic); }
/// <summary> /// Generates a sequence of initialize <see cref="StatementSyntax"/> for the specified sequence of <see cref="CommandElement"/>. /// </summary> /// <param name="commands">The sequence of <see cref="CommandElement"/> for which to generate the code.</param> /// <returns>The sequence of generated <see cref="StatementSyntax"/>.</returns> private static IEnumerable <StatementSyntax> GenerateInitializeStatements(IEnumerable <CommandElement> commands) { if (SyntaxFactory.ParseTypeName(DelegateCommand) is QualifiedNameSyntax typeName) { IdentifierNameSyntax parameterName = SyntaxFactory.IdentifierName(Parameter1Name); NameSyntax actionLeft = SyntaxFactory.ParseName(typeof(Action <object>).Namespace); GenericNameSyntax actionRight = SyntaxFactory.GenericName(nameof(Action <object>)); NameSyntax funcLeft = SyntaxFactory.ParseName(typeof(Func <object, bool>).Namespace); GenericNameSyntax funcRight = SyntaxFactory.GenericName(nameof(Func <object, bool>)); PredefinedTypeSyntax boolType = SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)); InvocationExpressionSyntax name = SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(SyntaxFactory.ParseToken(SyntaxFactory.Token(SyntaxKind.NameOfKeyword).ValueText))); foreach (CommandElement command in commands) { List <ArgumentSyntax> arguments = new List <ArgumentSyntax>(); arguments.Add( SyntaxFactory.Argument( SyntaxFactory.ObjectCreationExpression(SyntaxFactory.QualifiedName(actionLeft, actionRight.AddTypeArgumentListArguments(command.ParameterType))).AddArgumentListArguments( SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parameterName, SyntaxFactory.IdentifierName(command.Execute))) ) ) ); if (command.CanExecute.HasValue) { arguments.Add( SyntaxFactory.Argument( SyntaxFactory.ObjectCreationExpression(SyntaxFactory.QualifiedName(funcLeft, funcRight.AddTypeArgumentListArguments(command.ParameterType, boolType))).AddArgumentListArguments( SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parameterName, SyntaxFactory.IdentifierName(command.CanExecute.Value))) ) ) ); } arguments.Add(SyntaxFactory.Argument(name.AddArgumentListArguments(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(command.Identifier))))); TypeSyntax type = typeName.WithRight(SyntaxFactory.GenericName(typeName.Right.Identifier).AddTypeArgumentListArguments(command.ParameterType)); yield return(SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(command.Identifier), SyntaxFactory.InvocationExpression(SyntaxFactory.ObjectCreationExpression(type)).AddArgumentListArguments(arguments.ToArray()) ) )); } } }