Ejemplo n.º 1
0
        /// <summary>
        /// Generate a field declaration syntax as if it was declared in a class implementation.
        /// </summary>
        /// <param name="type">The field type.</param>
        /// <param name="name">The field name.</param>
        /// <param name="init">The field initializer.</param>
        /// <returns>The FieldDeclarationSyntax node.</returns>
        public static FieldDeclarationSyntax GetFieldSyntax(string type, string name, string init)
        {
            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"public {type} {name} = {init};");

            return((FieldDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate a property declaration syntax as if it was declared in a class implementation with
        /// an expression body.
        /// </summary>
        /// <param name="type">The property type.</param>
        /// <param name="name">The property name.</param>
        /// <param name="expressionBody">The expression body.</param>
        /// <returns>The PropertyDeclarationSyntax node.</returns>
        public static PropertyDeclarationSyntax GetExpressionBodyPropertyImplSyntax(string type, string name, string expressionBody)
        {
            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"public {type} {name} => {expressionBody};");

            return((PropertyDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generate a syntax node given the declaration use code statement.
        /// </summary>
        /// <param name="typeStatement">The CSharp code to generate the type syntax node from.</param>
        /// <returns>The syntax node.</returns>
        public static TypeSyntax GetTypeSyntax(string typeStatement)
        {
            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"{typeStatement} a;");

            var field = (FieldDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single();

            return(field.Declaration.Type);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generate a local declaration syntax as if it was declared in a method implementation
        /// (for a variable declaration by instance).
        /// </summary>
        /// <param name="type">The variable type.</param>
        /// <param name="name">The variable name.</param>
        /// <returns>The VariableDeclarationSyntax node.</returns>
        public static LocalDeclarationStatementSyntax GetLocalDeclarationStatementSyntax(string type, string name, string init)
        {
            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"void Method(){{{type} {name} = {init};}}");

            var statementSyntax = ((MethodDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single()).Body.Statements.First();

            return((LocalDeclarationStatementSyntax)statementSyntax);
        }
Ejemplo n.º 5
0
        private static T AssertSingleMemberOfType <T>(StringBuilder output)
            where T : SyntaxNode
        {
            var resultingNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                                .GetSyntaxNode(output.ToString());

            var cun    = Assert.IsType <CompilationUnitSyntax>(resultingNode);
            var member = Assert.Single(cun.Members);

            return(Assert.IsType <T>(member));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generate a method declaration syntax as if it was declared in a class implementation.
        /// </summary>
        /// <param name="type">The method return type.</param>
        /// <param name="name">The method name.</param>
        /// <param name="argument">The argument name.</param>
        /// <returns>The MethodDeclarationSyntax node.</returns>
        public static MethodDeclarationSyntax GetMethodSyntax(string type, string name, string argument, string otherArguments)
        {
            if (otherArguments == null)
            {
                otherArguments = string.Empty;
            }

            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"public {type} {name}({type} {argument}{otherArguments}){{}}");

            return((MethodDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single());
        }
Ejemplo n.º 7
0
        private static IReadOnlyList <T> AssertMultiMemberOfType <T>(StringBuilder output)
            where T : SyntaxNode
        {
            var resultingNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                                .GetSyntaxNode(output.ToString());

            var cun     = Assert.IsType <CompilationUnitSyntax>(resultingNode);
            var results = new List <T>();

            foreach (var member in cun.Members)
            {
                results.Add(Assert.IsType <T>(member));
            }

            return(results);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Generate a property declaration syntax as if it was declared in a class implementation.
        /// </summary>
        /// <param name="type">The property type.</param>
        /// <param name="name">The property name.</param>
        /// <param name="field">The field to get or set.</param>
        /// <returns>The PropertyDeclarationSyntax node.</returns>
        public static PropertyDeclarationSyntax GetPropertyImplSyntax(string type, string name, string field = null)
        {
            var getImpl = "get;";
            var setImpl = "set;";

            if (!string.IsNullOrEmpty(field))
            {
                getImpl = $"get {{ return {field}; }}";
                setImpl = $"set {{ {field} = value; }}";
            }

            var syntaxNode = AReflectionSyntaxNodeProvider <SyntaxNode>
                             .GetSyntaxNode($"public {type} {name} {{ {getImpl} {setImpl} }}");

            return((PropertyDeclarationSyntax)((CompilationUnitSyntax)syntaxNode).Members.Single());
        }