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


C# ExpressionSyntax.FirstAncestorOrSelf方法代码示例

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


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

示例1: CreateConditional

        protected ExpressionSyntax CreateConditional(ExpressionSyntax whenTrue, ExpressionSyntax whenFalse, ITypeSymbol targetType)
        {
            Debug.Assert(whenTrue != null);
            Debug.Assert(whenTrue.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(whenFalse != null);
            Debug.Assert(whenFalse.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
            Debug.Assert(targetType != null);

            // If there is no conversion between when-true and when-false, we need to insert a cast in
            // one or both of the branches.
            if (!ConversionExists(whenTrue, whenFalse))
            {
                var whenTrueConversion = SemanticModel.ClassifyConversion(whenTrue, targetType);
                var whenFalseConversion = SemanticModel.ClassifyConversion(whenFalse, targetType);

                if (whenTrueConversion.IsExplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
                else if (whenFalseConversion.IsExplicit)
                {
                    whenFalse = whenFalse.CastTo(targetType);
                }
                else if (whenTrueConversion.IsImplicit && whenFalseConversion.IsImplicit)
                {
                    whenTrue = whenTrue.CastTo(targetType);
                }
            }

            var condition = IfStatement.Condition.Kind() == SyntaxKind.SimpleAssignmentExpression
                ? IfStatement.Condition.Parenthesize()
                : IfStatement.Condition;

            ExpressionSyntax result = SyntaxFactory.ConditionalExpression(condition, whenTrue, whenFalse);

            // Ensure that the conditional is implicitly convertible to the target type; otherwise,
            // insert a cast. We do this be speculatively determining the conversion classification
            // of the conditional expression to the target type in the same scope as the original
            // if-statement.
            var conversion = SemanticModel.ClassifyConversion(IfStatement.Span.Start, result, targetType);
            if (conversion.IsExplicit)
            {
                result = result.CastTo(targetType);
            }

            return result;
        }
开发者ID:Rickinio,项目名称:roslyn,代码行数:47,代码来源:ConditionalAnalyzer.cs

示例2: InsideNameOfExpression

        private static bool InsideNameOfExpression(ExpressionSyntax expr, SemanticModel semanticModel)
        {
            var nameOfInvocationExpr = expr.FirstAncestorOrSelf<InvocationExpressionSyntax>(
                invocationExpr =>
                {
                    var expression = invocationExpr.Expression as IdentifierNameSyntax;
                    return (expression != null) && (expression.Identifier.Text == "nameof") &&
                        semanticModel.GetConstantValue(invocationExpr).HasValue &&
                        (semanticModel.GetTypeInfo(invocationExpr).Type.SpecialType == SpecialType.System_String);
                });

            return nameOfInvocationExpr != null;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:13,代码来源:ExpressionSyntaxExtensions.cs

示例3: InsideCrefReference

 private static bool InsideCrefReference(ExpressionSyntax expr)
 {
     var crefAttribute = expr.FirstAncestorOrSelf<XmlCrefAttributeSyntax>();
     return crefAttribute != null;
 }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:5,代码来源:ExpressionSyntaxExtensions.cs

示例4: WillConflictWithExistingLocal

        private static bool WillConflictWithExistingLocal(ExpressionSyntax expression, ExpressionSyntax simplifiedNode)
        {
            if (simplifiedNode.Kind() == SyntaxKind.IdentifierName && !SyntaxFacts.IsInNamespaceOrTypeContext(expression))
            {
                var identifierName = (IdentifierNameSyntax)simplifiedNode;
                var enclosingDeclarationSpace = FindImmediatelyEnclosingLocalVariableDeclarationSpace(expression);
                var enclosingMemberDeclaration = expression.FirstAncestorOrSelf<MemberDeclarationSyntax>();
                if (enclosingDeclarationSpace != null && enclosingMemberDeclaration != null)
                {
                    var locals = enclosingMemberDeclaration.GetLocalDeclarationMap()[identifierName.Identifier.ValueText];
                    foreach (var token in locals)
                    {
                        if (token.GetAncestors<SyntaxNode>().Contains(enclosingDeclarationSpace))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:22,代码来源:ExpressionSyntaxExtensions.cs

示例5: IsRethrowOfCaughtException

            private static bool IsRethrowOfCaughtException(ExpressionSyntax thrown)
            {
                var containingCatch = thrown.FirstAncestorOrSelf<CatchClauseSyntax>();
                if (containingCatch != null)
                {
                    var catchDecl = containingCatch.Declaration;
                    if (catchDecl != null)
                    {
                        var caughtExId = catchDecl.Identifier;
                        if (thrown.IsKind(SyntaxKind.IdentifierName) && ((IdentifierNameSyntax)thrown).Identifier.IsEquivalentTo(caughtExId))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
开发者ID:itowlson,项目名称:torment-roslyn,代码行数:18,代码来源:Program.cs

示例6: IsObtainedFromFactory

 private bool IsObtainedFromFactory(ExpressionSyntax thrown)
 {
     if (thrown.IsKind(SyntaxKind.IdentifierName))
     {
         // throw SomeException; - exception property or field in same class
         IdentifierNameSyntax name = (IdentifierNameSyntax)thrown;
         var referencedSymbol = _semanticModel.GetSymbolInfo(name).Symbol;
         var referencedSyntax = referencedSymbol.DeclaringSyntaxReferences.Single().GetSyntax();
         var variableDecl = referencedSyntax as VariableDeclaratorSyntax;
         if (variableDecl != null && variableDecl.Initializer != null)
         {
             var val = variableDecl.Initializer.Value;
             return IsObtainedFromFactory(val);
         }
         else
         {
             var propertyDecl = referencedSyntax as PropertyDeclarationSyntax;
             if (propertyDecl != null)
             {
                 // since it is a bald declaration, it must be in the same type and hence the same assembly,
                 // and will therefore get picked up in the ObjectCreationExpressionSyntax handler
                 return true;
             }
         }
     }
     if (thrown.IsKind(SyntaxKind.InvocationExpression))
     {
         // throw Util.SomeException(task); - exception factory method
         InvocationExpressionSyntax expr = (InvocationExpressionSyntax)thrown;
         if (expr.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
         {
             return IsObtainedFromFactory(expr.Expression);
         }
     }
     if (thrown.IsKind(SyntaxKind.SimpleMemberAccessExpression))
     {
         // throw Util.SomeException - exception property or field in other class
         MemberAccessExpressionSyntax expr = (MemberAccessExpressionSyntax)thrown;
         if (expr.Expression.IsKind(SyntaxKind.IdentifierName))
         {
             var typeSyntaxWhereThrowIsHappening = thrown.FirstAncestorOrSelf<TypeDeclarationSyntax>();
             var typeWhereThrowIsHappening = _semanticModel.GetDeclaredSymbol(typeSyntaxWhereThrowIsHappening);
             var type = _semanticModel.GetTypeInfo(expr.Expression);
             if (type.Type.ContainingAssembly == typeWhereThrowIsHappening.ContainingAssembly)
             {
                 return true;
             }
         }
     }
     return false;
 }
开发者ID:itowlson,项目名称:torment-roslyn,代码行数:51,代码来源:Program.cs


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