Exemplo n.º 1
0
        private static string GetCountOrLengthPropertyName(
            ExpressionSyntax expression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ITypeSymbol typeSymbol = semanticModel
                                     .GetTypeInfo(expression, cancellationToken)
                                     .Type;

            if (typeSymbol?.IsErrorType() == false &&
                !SyntaxAnalyzer.IsGenericIEnumerable(typeSymbol))
            {
                if (typeSymbol.BaseType?.SpecialType == SpecialType.System_Array)
                {
                    return("Length");
                }

                if (SyntaxAnalyzer.IsGenericImmutableArray(typeSymbol, semanticModel))
                {
                    return("Length");
                }

                ImmutableArray <INamedTypeSymbol> allInterfaces = typeSymbol.AllInterfaces;

                for (int i = 0; i < allInterfaces.Length; i++)
                {
                    if (allInterfaces[i].ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_ICollection_T)
                    {
                        foreach (ISymbol members in typeSymbol.GetMembers("Count"))
                        {
                            if (members.IsProperty() &&
                                members.IsPublic())
                            {
                                return("Count");
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private static bool IsImmutableArrayElementAtMethod(
            InvocationExpressionSyntax invocation,
            SemanticModel semanticModel)
        {
            var methodSymbol = semanticModel
                               .GetSymbolInfo(invocation)
                               .Symbol as IMethodSymbol;

            if (methodSymbol?.ReducedFrom != null)
            {
                methodSymbol = methodSymbol.ReducedFrom;

                return(methodSymbol.MetadataName == "ElementAt" &&
                       methodSymbol.Parameters.Length == 2 &&
                       methodSymbol.ContainingType?.Equals(semanticModel.Compilation.GetTypeByMetadataName("System.Linq.ImmutableArrayExtensions")) == true &&
                       SyntaxAnalyzer.IsGenericImmutableArray(methodSymbol.Parameters[0].Type, semanticModel) &&
                       methodSymbol.Parameters[1].Type.IsInt32());
            }

            return(false);
        }