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


C# ExpressionSyntax.GetLocation方法代码示例

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


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

示例1: IsComplexCondition

        public static bool IsComplexCondition(ExpressionSyntax expr)
        {
            var loc = expr.GetLocation().GetLineSpan();
            if (loc.StartLinePosition.Line != loc.EndLinePosition.Line)
                return true;

            if (expr is LiteralExpressionSyntax || expr is IdentifierNameSyntax || expr is MemberAccessExpressionSyntax || expr is InvocationExpressionSyntax)
                return false;

            var pexpr = expr as ParenthesizedExpressionSyntax;
            if (pexpr != null)
                return IsComplexCondition(pexpr.Expression);

            var uOp = expr as PrefixUnaryExpressionSyntax;
            if (uOp != null)
                return IsComplexCondition(uOp.Operand);

            var bop = expr as BinaryExpressionSyntax;
            if (bop == null)
                return true;
            return !(bop.IsKind(SyntaxKind.GreaterThanExpression) ||
                bop.IsKind(SyntaxKind.GreaterThanOrEqualExpression) ||
                bop.IsKind(SyntaxKind.EqualsExpression) ||
                bop.IsKind(SyntaxKind.NotEqualsExpression) ||
                bop.IsKind(SyntaxKind.LessThanExpression) ||
                bop.IsKind(SyntaxKind.LessThanOrEqualExpression));
        }
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:27,代码来源:ConvertIfStatementToConditionalTernaryExpressionAnalyzer.cs

示例2: IsComplexExpression

 public static bool IsComplexExpression(ExpressionSyntax expr)
 {
     var loc = expr.GetLocation().GetLineSpan();
     return loc.StartLinePosition.Line != loc.EndLinePosition.Line ||
         expr is ConditionalExpressionSyntax ||
         expr is BinaryExpressionSyntax;
 }
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:7,代码来源:ConvertIfStatementToConditionalTernaryExpressionAnalyzer.cs

示例3: CheckForTypeConversion

 private static void CheckForTypeConversion(ExpressionSyntax expression, TypeInfo typeInfo, Action<Diagnostic> reportDiagnostic, string filePath)
 {
     if (typeInfo.Type != null && typeInfo.Type.IsValueType && typeInfo.ConvertedType != null && !typeInfo.ConvertedType.IsValueType)
     {
         reportDiagnostic(Diagnostic.Create(ValueTypeToReferenceTypeInAStringConcatenationRule, expression.GetLocation(), new object[] { typeInfo.Type.ToDisplayString() }));
         HeapAllocationAnalyzerEventSource.Logger.BoxingAllocationInStringConcatenation(filePath);
     }
 }
开发者ID:Maxwe11,项目名称:RoslynClrHeapAllocationAnalyzer,代码行数:8,代码来源:ConcatenationAllocationAnalyzer.cs

示例4: Check

        void Check(SyntaxNodeAnalysisContext nodeContext, ExpressionSyntax condition)
        {
            if (condition == null)
                return;

            if (condition.IsKind(SyntaxKind.TrueLiteralExpression) || condition.IsKind(SyntaxKind.FalseLiteralExpression))
                return;

            var resolveResult = nodeContext.SemanticModel.GetConstantValue(condition);
            if (!resolveResult.HasValue || !(resolveResult.Value is bool))
                return;

            var value = (bool)resolveResult.Value;

            nodeContext.ReportDiagnostic(Diagnostic.Create(
                descriptor.Id,
                descriptor.Category,
                string.Format(descriptor.MessageFormat.ToString(), value),
                descriptor.DefaultSeverity,
                descriptor.DefaultSeverity,
                descriptor.IsEnabledByDefault,
                4,
                descriptor.Title,
                descriptor.Description,
                descriptor.HelpLinkUri,
                condition.GetLocation(),
                null,
                new[] { value.ToString() }
            ));
        }
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:30,代码来源:ConstantConditionAnalyzer.cs

示例5: BindSwitchExpression

        // Bind the switch expression
        private BoundExpression BindSwitchExpression(ExpressionSyntax node, DiagnosticBag diagnostics)
        {
            // We are at present inside the switch binder, but the switch expression is not
            // bound in the context of the switch binder; it's bound in the context of the
            // enclosing binder. For example: 
            // 
            // class C { 
            //   int x; 
            //   void M() {
            //     switch(x) {
            //       case 1:
            //         int x;
            //
            // The "x" in "switch(x)" refers to this.x, not the local x that is in scope inside the switch block.

            Debug.Assert(node == SwitchSyntax.Expression);
            var binder = this.GetBinder(node);
            Debug.Assert(binder != null);

            var switchExpression = binder.BindValue(node, diagnostics, BindValueKind.RValue);

            var switchGoverningType = switchExpression.Type;

            if ((object)switchGoverningType != null && !switchGoverningType.IsErrorType())
            {
                // SPEC:    The governing type of a switch statement is established by the switch expression.
                // SPEC:    1) If the type of the switch expression is sbyte, byte, short, ushort, int, uint,
                // SPEC:       long, ulong, bool, char, string, or an enum-type, or if it is the nullable type
                // SPEC:       corresponding to one of these types, then that is the governing type of the switch statement. 
                // SPEC:    2) Otherwise if exactly one user-defined implicit conversion (§6.4) exists from the
                // SPEC:       type of the switch expression to one of the following possible governing types:
                // SPEC:       sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type
                // SPEC:       corresponding to one of those types, then the result is the switch governing type
                // SPEC:    3) Otherwise (in C# 7 and later) the switch governing type is the type of the
                // SPEC:       switch expression.

                if (switchGoverningType.IsValidV6SwitchGoverningType())
                {
                    // Condition (1) satisfied

                    // Note: dev11 actually checks the stripped type, but nullable was introduced at the same
                    // time, so it doesn't really matter.
                    if (switchGoverningType.SpecialType == SpecialType.System_Boolean)
                    {
                        // GetLocation() so that it also works in speculative contexts.
                        CheckFeatureAvailability(node.GetLocation(), MessageID.IDS_FeatureSwitchOnBool, diagnostics);
                    }

                    return switchExpression;
                }
                else
                {
                    TypeSymbol resultantGoverningType;
                    HashSet<DiagnosticInfo> useSiteDiagnostics = null;
                    Conversion conversion = binder.Conversions.ClassifyImplicitUserDefinedConversionForV6SwitchGoverningType(switchGoverningType, out resultantGoverningType, ref useSiteDiagnostics);
                    diagnostics.Add(node, useSiteDiagnostics);
                    if (conversion.IsValid)
                    {
                        // Condition (2) satisfied
                        Debug.Assert(conversion.Kind == ConversionKind.ImplicitUserDefined);
                        Debug.Assert(conversion.Method.IsUserDefinedConversion());
                        Debug.Assert(conversion.UserDefinedToConversion.IsIdentity);
                        Debug.Assert(resultantGoverningType.IsValidV6SwitchGoverningType(isTargetTypeOfUserDefinedOp: true));
                        return binder.CreateConversion(node, switchExpression, conversion, false, resultantGoverningType, diagnostics);
                    }
                    else if (switchGoverningType.SpecialType != SpecialType.System_Void && PatternsEnabled)
                    {
                        // Otherwsie (3) satisfied
                        return switchExpression;
                    }
                    else
                    {
                        // We need to create an error type here as certain diagnostics generated during binding the switch case label expression and
                        // goto case expression should be generated only if the switch expression type is a valid switch governing type.
                        switchGoverningType = CreateErrorType(switchGoverningType.Name);
                    }
                }
            }

            if (!switchExpression.HasAnyErrors)
            {
                if (PatternsEnabled)
                {
                    diagnostics.Add(ErrorCode.ERR_PatternValueExpected, node.Location, switchExpression.Display);
                }
                else
                {
                    diagnostics.Add(ErrorCode.ERR_V6SwitchGoverningTypeValueExpected, node.Location);
                }
            }

            return new BoundBadExpression(node, LookupResultKind.Empty, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundNode>(switchExpression), switchGoverningType ?? CreateErrorType());
        }
开发者ID:vslsnap,项目名称:roslyn,代码行数:94,代码来源:SwitchBinder.cs

示例6: CheckAssignment

 private void CheckAssignment(NullState variableState, NullState expressionState, ExpressionSyntax expression)
 {
     if (variableState == NullState.ShouldNotBeNull)
     {
         switch (expressionState)
         {
             case NullState.Null:
             case NullState.CouldBeNull:
                 context.ReportDiagnostic(Diagnostic.Create(PossibleNullAssignment, expression.GetLocation()));
                 break;
         }
     }
 }
开发者ID:uxsoft,项目名称:nullaby,代码行数:13,代码来源:NullAnalyzer.cs

示例7: ProcessPropertyChange

        private static void ProcessPropertyChange(ExpressionSyntax expression, SemanticModel semanticModel,
            SyntaxNodeAnalysisContext context)
        {
            var memberAccess = expression as MemberAccessExpressionSyntax;
            if (memberAccess == null)
            {
                return;
            }

            var propertySymbol = semanticModel.GetSymbolInfo(expression).Symbol as IPropertySymbol;
            if (propertySymbol == null)
            {
                return;
            }

            var fieldSymbol = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol as IFieldSymbol;
            if (fieldSymbol == null ||
                !fieldSymbol.IsReadOnly ||
                !RelevantFieldType(fieldSymbol.Type))
            {
                return;
            }

            var constructorSymbol = semanticModel.GetEnclosingSymbol(expression.SpanStart) as IMethodSymbol;
            if (constructorSymbol != null &&
                constructorSymbol.MethodKind == MethodKind.Constructor &&
                constructorSymbol.ContainingType.Equals(fieldSymbol.ContainingType))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation(), fieldSymbol.Name, propertySymbol.Name));
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:33,代码来源:GenericReadonlyFieldPropertyAssignment.cs

示例8: CheckExpressionForPureMethod

        private static void CheckExpressionForPureMethod(SyntaxNodeAnalysisContext c, ExpressionSyntax expression)
        {
            var invocation = expression as InvocationExpressionSyntax;
            if (invocation == null)
            {
                return;
            }

            var invokedMethodSymbol = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
            if (invokedMethodSymbol == null ||
                invokedMethodSymbol.ReturnsVoid)
            {
                return;
            }

            if (invokedMethodSymbol.Parameters.All(p => p.RefKind == RefKind.None) &&
                IsSideEffectFreeOrPure(invokedMethodSymbol))
            {
                c.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation(), invokedMethodSymbol.Name));
            }
        }
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:21,代码来源:ReturnValueIgnored.cs

示例9: ProcessPropertyChange

        private static void ProcessPropertyChange(ExpressionSyntax expression, SemanticModel semanticModel, 
            SyntaxNodeAnalysisContext context)
        {
            var memberAccess = expression as MemberAccessExpressionSyntax;
            if (memberAccess == null)
            {
                return;
            }

            var propertySymbol = semanticModel.GetSymbolInfo(expression).Symbol as IPropertySymbol;
            if (propertySymbol == null)
            {
                return;
            }

            var fieldSymbol = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol as IFieldSymbol;
            if (fieldSymbol == null ||
                !fieldSymbol.IsReadOnly ||
                !RelevantFieldType(fieldSymbol.Type))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation(), fieldSymbol.Name, propertySymbol.Name));
        }
开发者ID:OsirisTerje,项目名称:sonarlint-vs,代码行数:25,代码来源:GenericReadonlyFieldPropertyAssignment.cs

示例10: ProcessPropertyChange

        private static void ProcessPropertyChange(ExpressionSyntax expression, SemanticModel semanticModel,
            SyntaxNodeAnalysisContext context)
        {
            var memberAccess = expression as MemberAccessExpressionSyntax;
            if (memberAccess == null)
            {
                return;
            }

            var propertySymbol = semanticModel.GetSymbolInfo(expression).Symbol as IPropertySymbol;
            if (propertySymbol == null)
            {
                return;
            }

            var fieldSymbol = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol as IFieldSymbol;
            if (!IsFieldReadonlyAndPossiblyValueType(fieldSymbol) ||
                IsInsideConstructorDeclaration(expression, fieldSymbol.ContainingType, semanticModel))
            {
                return;
            }

            context.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation(), fieldSymbol.Name, propertySymbol.Name));
        }
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:24,代码来源:GenericReadonlyFieldPropertyAssignment.cs

示例11: CollectLocationOfStaticField

 private static void CollectLocationOfStaticField(ExpressionSyntax expression, Dictionary<IFieldSymbol, List<Location>> locationsForFields, SyntaxNodeAnalysisContext context)
 {
     var fieldSymbol = context.SemanticModel.GetSymbolInfo(expression).Symbol as IFieldSymbol;
     if (IsStatic(fieldSymbol))
     {
         AddFieldLocation(fieldSymbol, expression.GetLocation(), locationsForFields);
     }
 }
开发者ID:duncanpMS,项目名称:sonarlint-vs,代码行数:8,代码来源:StaticFieldWrittenFromInstanceMember.cs


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