Exemplo n.º 1
0
        protected virtual IEnumerable <MemberDeclarationSyntax> CreateConstructors()
        {
            yield return(SF.ConstructorDeclaration
                         (
                             SF.List <AttributeListSyntax>(),

                             // Only Amazon.JSII.Runtime should create interface proxies,
                             // so we make the constructor private.
                             SF.TokenList(SF.Token(SyntaxKind.PrivateKeyword)),
                             GetProxyTypeNameSyntax(),
                             SF.ParseParameterList("(ByRefValue reference)"),
                             SF.ConstructorInitializer
                             (
                                 SyntaxKind.BaseConstructorInitializer,
                                 SF.ParseArgumentList("(reference)")
                             ),
                             SF.Block(),
                             null
                         ));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates constructors.
        /// </summary>
        /// <param name="className">The class name.</param>
        /// <returns>Constructor syntax for the provided class name.</returns>
        private static MemberDeclarationSyntax[] GenerateConstructors(string className)
        {
            var baseConstructors =
                typeof(GrainReference).GetTypeInfo().GetConstructors(
                    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(_ => !_.IsPrivate);
            var constructors = new List <MemberDeclarationSyntax>();

            foreach (var baseConstructor in baseConstructors)
            {
                var args = baseConstructor.GetParameters()
                           .Select(arg => SF.Argument(arg.Name.ToIdentifierName()))
                           .ToArray();
                var declaration =
                    baseConstructor.GetDeclarationSyntax(className)
                    .WithInitializer(
                        SF.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer)
                        .AddArgumentListArguments(args))
                    .AddBodyStatements();
                constructors.Add(declaration);
            }

            return(constructors.ToArray());
        }
Exemplo n.º 3
0
 public ConstructorBuilder CallThis(IEnumerable <ExpressionBuilder> parameters)
 => new ConstructorBuilder(_modifiers, _name, _parameters,
                           SF.ConstructorInitializer(SyntaxKind.ThisConstructorInitializer,
                                                     SF.ArgumentList(SF.SeparatedList(parameters.Select(p => SF.Argument(p.Build()))))),
                           _expr, _block);
Exemplo n.º 4
0
        IEnumerable <MemberDeclarationSyntax> CreateConstructors()
        {
            SyntaxToken typeName = Symbols.GetNameSyntaxToken(Type);

            if (Type.Initializer != null)
            {
                yield return(SF.ConstructorDeclaration
                             (
                                 SF.List <AttributeListSyntax>(),
                                 SF.TokenList(SF.Token(
                                                  Type.IsAbstract || Type.Initializer.IsProtected
                            ? SyntaxKind.ProtectedKeyword
                            : SyntaxKind.PublicKeyword
                                                  )),
                                 typeName,
                                 Type.Initializer.GetParameterListSyntax(Namespaces, Symbols),
                                 SF.ConstructorInitializer
                                 (
                                     SyntaxKind.BaseConstructorInitializer,
                                     SF.ArgumentList(
                                         SF.SeparatedList(new[]
                {
                    SF.Argument(
                        SF.ObjectCreationExpression(
                            SF.Token(SyntaxKind.NewKeyword),
                            SF.ParseTypeName("DeputyProps"),
                            SF.ArgumentList(SF.SeparatedList(
                                                new[] { GetBaseArgument() }
                                                )),
                            null
                            )
                        )
                })
                                         )
                                 ),
                                 SF.Block(),
                                 null
                             ));
            }

            yield return(SF.ConstructorDeclaration
                         (
                             SF.List <AttributeListSyntax>(),
                             SF.TokenList(SF.Token(SyntaxKind.ProtectedKeyword)),
                             typeName,
                             SF.ParseParameterList("(ByRefValue reference)"),
                             SF.ConstructorInitializer
                             (
                                 SyntaxKind.BaseConstructorInitializer,
                                 SF.ParseArgumentList("(reference)")
                             ),
                             SF.Block(),
                             null
                         ));

            // This constructor allows child classes to supply their own parameter lists. It is always protected.
            yield return(SF.ConstructorDeclaration
                         (
                             SF.List <AttributeListSyntax>(),
                             SF.TokenList(SF.Token(SyntaxKind.ProtectedKeyword)),
                             typeName,
                             SF.ParseParameterList("(DeputyProps props)"),
                             SF.ConstructorInitializer
                             (
                                 SyntaxKind.BaseConstructorInitializer,
                                 SF.ParseArgumentList("(props)")
                             ),
                             SF.Block(),
                             null
                         ));

            ArgumentSyntax GetBaseArgument()
            {
                var deputyArguments = (Type.Initializer.Parameters ?? Enumerable.Empty <Parameter>())
                                      .Select(p => Symbols.GetNameSyntaxToken(p))
                                      .Select(i => SF.IdentifierName(i));

                // In C#, arrays of reference types are covariant. Because of this, passing a string[]
                // to a method that takes `params object[] args` will interperet the string array *as*
                // args, rather than as args' first element. To workaround with, we remove the params
                // keyword from DeputyBase's constructor, and always explicitly create an array of
                // objects when calling it.
                return(SF.Argument(
                           SF.ArrayCreationExpression(
                               SF.Token(SyntaxKind.NewKeyword),
                               SF.ArrayType(SF.ParseTypeName("object[]")),
                               SF.InitializerExpression(
                                   SyntaxKind.ArrayInitializerExpression,
                                   SF.SeparatedList <ExpressionSyntax>(deputyArguments)
                                   )
                               )
                           ));
            }
        }