public static string GetCountOrLengthPropertyName(
            ITypeSymbol typeSymbol,
            SemanticModel semanticModel,
            int position)
        {
            SymbolKind kind = typeSymbol.Kind;

            if (kind == SymbolKind.ErrorType)
            {
                return(null);
            }

            if (kind == SymbolKind.ArrayType)
            {
                return("Length");
            }

            string propertyName = GetCountOrLengthPropertyName(typeSymbol.SpecialType);

            if (propertyName != null)
            {
                return((propertyName.Length > 0) ? propertyName : null);
            }

            ITypeSymbol originalDefinition = typeSymbol.OriginalDefinition;

            if (!typeSymbol.Equals(originalDefinition))
            {
                propertyName = GetCountOrLengthPropertyName(originalDefinition.SpecialType);

                if (propertyName != null)
                {
                    return((propertyName.Length > 0) ? propertyName : null);
                }
            }

            if (originalDefinition.ImplementsAny(
                    SpecialType.System_Collections_Generic_ICollection_T,
                    SpecialType.System_Collections_Generic_IReadOnlyCollection_T,
                    allInterfaces: true))
            {
                if (originalDefinition.TypeKind == TypeKind.Interface)
                {
                    return("Count");
                }

                foreach (ISymbol symbol in typeSymbol.GetMembers())
                {
                    if (symbol.Kind == SymbolKind.Property &&
                        StringUtility.Equals(symbol.Name, "Count", "Length") &&
                        semanticModel.IsAccessible(position, symbol))
                    {
                        return(symbol.Name);
                    }
                }
            }

            return(null);

            string GetCountOrLengthPropertyName(SpecialType specialType)
            {
                switch (specialType)
                {
                case SpecialType.System_String:
                case SpecialType.System_Array:
                    return("Length");

                case SpecialType.System_Collections_Generic_IList_T:
                case SpecialType.System_Collections_Generic_ICollection_T:
                case SpecialType.System_Collections_Generic_IReadOnlyList_T:
                case SpecialType.System_Collections_Generic_IReadOnlyCollection_T:
                    return("Count");

                case SpecialType.None:
                    return(null);
                }

                return("");
            }
        }
 public static bool IsPublicInstance(IPropertySymbol propertySymbol, string name = null)
 {
     return(propertySymbol?.DeclaredAccessibility == Accessibility.Public &&
            !propertySymbol.IsStatic &&
            StringUtility.IsNullOrEquals(name, propertySymbol.Name));
 }
Exemple #3
0
        public static string CreateName(ITypeSymbol typeSymbol)
        {
            if (typeSymbol == null)
            {
                throw new ArgumentNullException(nameof(typeSymbol));
            }

            if (typeSymbol.IsKind(SymbolKind.ErrorType, SymbolKind.DynamicType))
            {
                return(null);
            }

            ITypeSymbol typeSymbol2 = ExtractFromNullableType(typeSymbol);

            ITypeSymbol typeSymbol3 = ExtractFromArrayOrGenericCollection(typeSymbol2);

            string name = GetName(typeSymbol3);

            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

            if (typeSymbol3.TypeKind == TypeKind.Interface &&
                name.Length > 1 &&
                name[0] == 'I')
            {
                name = name.Substring(1);
            }

            if (name.Length >= 8 &&
                name.EndsWith("Syntax", StringComparison.Ordinal) &&
                typeSymbol.EqualsOrInheritsFrom(MetadataNames.Microsoft_CodeAnalysis_SyntaxNode))
            {
                name = name.Remove(name.Length - 6);
            }

            if (name.Length > 1 &&
                UsePlural(typeSymbol2))
            {
                if (!StringUtility.TryRemoveSuffix(name, "Collection", out name))
                {
                    StringUtility.TryRemoveSuffix(name, "List", out name);
                }

                if (name.EndsWith("s", StringComparison.Ordinal) || name.EndsWith("x", StringComparison.Ordinal))
                {
                    name += "es";
                }
                else if (name.EndsWith("y", StringComparison.Ordinal))
                {
                    name = name.Remove(name.Length - 1) + "ies";
                }
                else
                {
                    name += "s";
                }
            }

            return(name);
        }