Exemplo n.º 1
0
        public static string GenerateNameFromType(
            this SemanticModel semanticModel,
            ITypeSymbol type,
            ISyntaxFacts syntaxFacts,
            bool capitalize
            )
        {
            var pluralize     = semanticModel.ShouldPluralize(type);
            var typeArguments = type.GetAllTypeArguments();

            // We may be able to use the type's arguments to generate a name if we're working with an enumerable type.
            if (
                pluralize &&
                TryGeneratePluralizedNameFromTypeArgument(
                    syntaxFacts,
                    typeArguments,
                    capitalize,
                    out var typeArgumentParameterName
                    )
                )
            {
                return(typeArgumentParameterName);
            }

            // If there's no type argument and we have an array type, we should pluralize, e.g. using 'frogs' for 'new Frog[]' instead of 'frog'
            if (type.TypeKind == TypeKind.Array && typeArguments.IsEmpty)
            {
                return(type.CreateParameterName(capitalize).Pluralize());
            }

            // Otherwise assume no pluralization, e.g. using 'immutableArray', 'list', etc. instead of their
            // plural forms
            return(type.CreateParameterName(capitalize));
        }
            private static string TryRemoveInterfacePrefix(ITypeSymbol type)
            {
                var name = type.Name;

                if (type.TypeKind == TypeKind.Interface && name.Length > 1)
                {
                    if (name[0] == 'I' && char.IsLower(name[1]))
                    {
                        return(name.Substring(1));
                    }
                }
                return(type.CreateParameterName());
            }