当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNode.As方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.As方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.As方法的具体用法?C# SyntaxNode.As怎么用?C# SyntaxNode.As使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.SyntaxNode的用法示例。


在下文中一共展示了SyntaxNode.As方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Go

        private static void Go(OutputWriter writer, IEnumerable<ParameterSyntax> parameters, SyntaxNode body,
            TypeInfo type)
        {
            var methodSymbol = type.ConvertedType.As<INamedTypeSymbol>().DelegateInvokeMethod.As<IMethodSymbol>();

            var typeStringNoPtr = TypeProcessor.ConvertType(type.ConvertedType);

            if (type.ConvertedType.TypeKind == TypeKind.TypeParameter)
                writer.Write(" __TypeNew!(" + typeStringNoPtr + ")(");
            else
                writer.Write("new " + typeStringNoPtr + "(");

            writer.Write("(");

            var parameterSyntaxs = parameters as ParameterSyntax[] ?? parameters.ToArray();
            for (int pi = 0; pi < parameterSyntaxs.Count(); pi++)
            {
                var parameter = parameterSyntaxs.ElementAt(pi);
                if (pi > 0)
                    writer.Write(", ");

                if (parameter.Type != null)
                    writer.Write(TypeProcessor.ConvertType(parameter.Type) + " ");
                else
                    writer.Write(TypeProcessor.ConvertType(methodSymbol.Parameters[pi].Type) + " ");
                writer.Write(WriteIdentifierName.TransformIdentifier(parameter.Identifier.Text));
            }

            writer.Write(")");

            bool returnsVoid = methodSymbol.ReturnType.ToString() == "void";

            if (body is BlockSyntax)
            {
                writer.Write("\r\n");
                writer.OpenBrace();

                var statements = body.As<BlockSyntax>().Statements;

                var lastStatement = statements.LastOrDefault() as ReturnStatementSyntax;

                var returnStatements = FindReturnStatements(body);

                {
                    foreach (var statement in statements)
                    {
                        if (statement == lastStatement)
                        {
                            writer.WriteIndent();

                            Core.Write(writer, lastStatement);
//                            writer.Write(";\r\n");
                        }
                        else
                            Core.Write(writer, statement);
                    }
                }

                writer.Indent--;
                writer.WriteIndent();
                writer.Write("}");
            }
            else
            {
                if (!returnsVoid)
                {
                    writer.Write("=> ");
                    Core.Write(writer, body);
                }
                else
                {
                    writer.Write(" { ");
                    Core.Write(writer, body);
                    writer.Write("; }");
                }
                //writer.Write(" { ");
                //if (!returnsVoid)
                //	writer.Write ("return ");
                //    writer.Write("; }");
            }

//            if (!returnsVoid)
//                writer.Write(TypeProcessor.ConvertTypeWithColon(methodSymbol.ReturnType));
            writer.Write(")");
        }
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:85,代码来源:WriteLambdaExpression.cs

示例2: Factory

        private static void Factory(OutputWriter writer, SyntaxNode node, bool isConst)
        {
            if (node is ConstructorInitializerSyntax)
                WriteConstructorInitializer.Go(writer, node.As<ConstructorInitializerSyntax>());
            else if (node is CheckedExpressionSyntax)
                WriteChecked.Go(writer, node.As<CheckedExpressionSyntax>());
            else if (node is CheckedStatementSyntax)
                WriteChecked.Go(writer, node.As<CheckedStatementSyntax>());
            else if (node is UnsafeStatementSyntax)
                WriteUnsafeStatement.Go(writer, node.As<UnsafeStatementSyntax>());
            else if (node is InitializerExpressionSyntax)
                WriteInitializer.Go(writer, node.As<InitializerExpressionSyntax>());
            else if (node is GotoStatementSyntax)
                WriteGoto.Go(writer, node.As<GotoStatementSyntax>());
            else if (node is CaseSwitchLabelSyntax)
                WriteLabel.Go(writer, node.As<CaseSwitchLabelSyntax>());
            else if (node is LabeledStatementSyntax)
                WriteLabel.Go(writer, node.As<LabeledStatementSyntax>());
            else if (node is OperatorDeclarationSyntax)
                WriteOperatorDeclaration.Go(writer, node.As<OperatorDeclarationSyntax>());
            else if (node is MethodDeclarationSyntax)
                WriteMethod.Go(writer, node.As<MethodDeclarationSyntax>());
            else if (node is PropertyDeclarationSyntax)
                WriteProperty.Go(writer, node.As<PropertyDeclarationSyntax>());
            else if (node is EventDeclarationSyntax)
                WriteEvent.Go(writer, node.As<EventDeclarationSyntax>());
            else if (node is FieldDeclarationSyntax)
                WriteField.Go(writer, node.As<FieldDeclarationSyntax>());
            else if (node is EventFieldDeclarationSyntax)
                WriteField.Go(writer, node.As<EventFieldDeclarationSyntax>());
            else if (node is ConstructorDeclarationSyntax)
                WriteConstructorBody.Go(writer, node.As<ConstructorDeclarationSyntax>());
            else if (node is ExpressionStatementSyntax)
                WriteStatement(writer, node.As<ExpressionStatementSyntax>());
            else if (node is FixedStatementSyntax)
                WriteFixedStatement(writer, node.As<FixedStatementSyntax>());
            else if (node is LocalDeclarationStatementSyntax)
                WriteLocalDeclaration.Go(writer, node.As<LocalDeclarationStatementSyntax>());
            else if (node is VariableDeclarationSyntax)
                WriteVariableDeclaration.Go(writer, node.As<VariableDeclarationSyntax>());
            else if (node is BlockSyntax)
                WriteBlock(writer, node.As<BlockSyntax>());
            else if (node is InvocationExpressionSyntax)
                WriteInvocationExpression.Go(writer, node.As<InvocationExpressionSyntax>());
            else if (node is LiteralExpressionSyntax)
                WriteLiteralExpression.Go(writer, node.As<LiteralExpressionSyntax>(), isConst);
            else if (node is IdentifierNameSyntax)
                WriteIdentifierName.Go(writer, node.As<IdentifierNameSyntax>());
            else if (node is ImplicitArrayCreationExpressionSyntax)
                WriteArrayCreationExpression.Go(writer, node.As<ImplicitArrayCreationExpressionSyntax>());
            else if (node is ArrayCreationExpressionSyntax)
                WriteArrayCreationExpression.Go(writer, node.As<ArrayCreationExpressionSyntax>());
            else if (node is MemberAccessExpressionSyntax)
                WriteMemberAccessExpression.Go(writer, node.As<MemberAccessExpressionSyntax>());
            else if (node is ParenthesizedLambdaExpressionSyntax)
                WriteLambdaExpression.Go(writer, node.As<ParenthesizedLambdaExpressionSyntax>());
            else if (node is SimpleLambdaExpressionSyntax)
                WriteLambdaExpression.Go(writer, node.As<SimpleLambdaExpressionSyntax>());
            else if (node is AnonymousMethodExpressionSyntax)
                WriteLambdaExpression.Go(writer, node.As<AnonymousMethodExpressionSyntax>());
            else if (node is ReturnStatementSyntax)
                WriteReturnStatement.Go(writer, node.As<ReturnStatementSyntax>());
            else if (node is ObjectCreationExpressionSyntax)
                WriteObjectCreationExpression.Go(writer, node.As<ObjectCreationExpressionSyntax>());
            else if (node is ElementAccessExpressionSyntax)
                WriteElementAccessExpression.Go(writer, node.As<ElementAccessExpressionSyntax>());
            else if (node is ForEachStatementSyntax)
                WriteForEachStatement.Go(writer, node.As<ForEachStatementSyntax>());
            else if (node is IfStatementSyntax)
                WriteIfStatement.Go(writer, node.As<IfStatementSyntax>());
            else if (node is BinaryExpressionSyntax)
                WriteBinaryExpression.Go(writer, node.As<BinaryExpressionSyntax>());
            else if (node is AssignmentExpressionSyntax)
                WriteAssignmentExpression.Go(writer, node.As<AssignmentExpressionSyntax>());
            else if (node is ConditionalExpressionSyntax)
                WriteConditionalExpression.Go(writer, node.As<ConditionalExpressionSyntax>());
            else if (node is BaseExpressionSyntax)
                WriteBaseExpression.Go(writer, node.As<BaseExpressionSyntax>());
            else if (node is ThisExpressionSyntax)
                WriteThisExpression.Go(writer, node.As<ThisExpressionSyntax>());
            else if (node is CastExpressionSyntax)
                WriteCastExpression.Go(writer, node.As<CastExpressionSyntax>());
            else if (node is ThrowStatementSyntax)
                WriteThrowStatement.Go(writer, node.As<ThrowStatementSyntax>());
            else if (node is EqualsValueClauseSyntax)
                WriteEqualsValueClause.Go(writer, node.As<EqualsValueClauseSyntax>());
            else if (node is ForStatementSyntax)
                WriteForStatement.Go(writer, node.As<ForStatementSyntax>());
            else if (node is WhileStatementSyntax)
                WriteWhileStatement.Go(writer, node.As<WhileStatementSyntax>());
            else if (node is BreakStatementSyntax)
                WriteBreakStatement.Go(writer, node.As<BreakStatementSyntax>());
            else if (node is ContinueStatementSyntax)
                WriteContinueStatement.Go(writer, node.As<ContinueStatementSyntax>());
            else if (node is DoStatementSyntax)
                WriteDoStatement.Go(writer, node.As<DoStatementSyntax>());
            else if (node is SwitchStatementSyntax)
                WriteSwitchStatement.Go(writer, node.As<SwitchStatementSyntax>());
            else if (node is TryStatementSyntax)
                WriteTryStatement.Go(writer, node.As<TryStatementSyntax>());
//.........这里部分代码省略.........
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:101,代码来源:Core.cs

示例3: FindReturnStatements

        private static void FindReturnStatements(SyntaxNode node, List<ReturnStatementSyntax> ret)
        {
            if (node is ParenthesizedLambdaExpressionSyntax || node is SimpleLambdaExpressionSyntax)
                return; //any returns in a sub-lambda will be for that lambda. Ignore them.

            if (node is ReturnStatementSyntax)
                ret.Add(node.As<ReturnStatementSyntax>());

            foreach (var child in node.ChildNodes())
                FindReturnStatements(child, ret);
        }
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:11,代码来源:WriteLambdaExpression.cs


注:本文中的Microsoft.CodeAnalysis.SyntaxNode.As方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。