コード例 #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private EnumMemberDeclarationSyntax ToEnumMemberSyntax(EnumMember member)
        {
            var syntax = EnumMemberDeclaration(Identifier(member.Name));

            if (member.Value != null)
            {
                syntax = syntax.WithEqualsValue(EqualsValueClause(SyntaxHelpers.GetLiteralSyntax(member.Value)));
            }

            return(syntax);
        }
コード例 #2
0
        public static AttributeSyntax EmitSyntax(AttributeDescription description)
        {
            var syntax = Attribute(SyntaxHelpers.GetTypeFullNameSyntax(description.AttributeType));

            if (description.ConstructorArguments.Count > 0 || description.PropertyValues.Count > 0)
            {
                syntax = syntax
                         .WithArgumentList(
                    AttributeArgumentList(
                        SeparatedList <AttributeArgumentSyntax>(
                            description.ConstructorArguments.Select(arg =>
                                                                    AttributeArgument(SyntaxHelpers.GetLiteralSyntax(arg)))
                            .Concat(description.PropertyValues.Select(pv =>
                                                                      AttributeArgument(ExpressionSyntaxEmitter.EmitSyntax(pv.Value))
                                                                      .WithNameEquals(NameEquals(IdentifierName(pv.Name)))
                                                                      ))
                            )
                        )
                    );
            }

            return(syntax);
        }
コード例 #3
0
        public static ExpressionSyntax EmitSyntax(AbstractExpression expression)
        {
            if (expression is ConstantExpression constant)
            {
                return(SyntaxHelpers.GetLiteralSyntax(constant.Value));
            }
            if (expression is LocalVariableExpression local)
            {
                return(IdentifierName(local.Variable.Name));
            }
            if (expression is MethodCallExpression call)
            {
                return(EmitMethodCallSyntax(call));
            }
            if (expression is NewObjectExpression newObject)
            {
                return(EmitNewObjectSyntax(newObject));
            }
            if (expression is ThisExpression)
            {
                return(ThisExpression());
            }
            if (expression is BaseExpression)
            {
                return(BaseExpression());
            }
            if (expression is ParameterExpression argument)
            {
                return(IdentifierName(argument.Parameter.Name));
            }
            if (expression is AssignmentExpression assignment)
            {
                return(AssignmentExpression(
                           SyntaxKind.SimpleAssignmentExpression,
                           EmitSyntax(assignment.Left),
                           EmitSyntax(assignment.Right)));
            }
            if (expression is MemberExpression member)
            {
                return(MemberAccessExpression(
                           SyntaxKind.SimpleMemberAccessExpression,
                           EmitSyntax(member.Target),
                           IdentifierName(member.Member?.Name ?? member.MemberName)));
            }
            if (expression is NewArrayExpression newArray)
            {
                return(EmitNewArraySyntax(newArray));
            }
            if (expression is IndexerExpression indexer)
            {
                return(ElementAccessExpression(EmitSyntax(indexer.Target)).WithArgumentList(
                           BracketedArgumentList(
                               SeparatedList <ArgumentSyntax>(
                                   indexer.IndexArguments.Select(arg => Argument(EmitSyntax(arg)))))));
            }
            if (expression is BinaryExpression binary)
            {
                return(BinaryExpression(GetBinaryOperatorKeyword(binary.Operator), EmitSyntax(binary.Left), EmitSyntax(binary.Right)));
            }

            //TODO: support other types of expressions

            throw new NotSupportedException($"Syntax emitter is not supported for expression node of type '{expression.GetType().Name}'.");
        }