Beispiel #1
0
        public static ITypeSymbol GetElementType(ITypeSymbol collectionType)
        {
            switch (collectionType)
            {
            case INamedTypeSymbol namedType:
                if (namedType.IsGenericType == false)
                {
                    if (namedType.BaseType == null)
                    {
                        var indexer = namedType.GetMembers(WellKnownMemberNames.Indexer).OfType <IPropertySymbol>().FirstOrDefault();
                        if (indexer != null)
                        {
                            return(indexer.Type);
                        }

                        throw new NotSupportedException("Cannot determine collection element type");
                    }
                    if (ObjectHelper.IsSystemObject(namedType.BaseType))
                    {
                        return(namedType.BaseType);
                    }
                    return(GetElementType(namedType.BaseType));
                }
                return(namedType.TypeArguments[0]);

            case IArrayTypeSymbol arrayType:
                return(arrayType.ElementType);

            default:
                throw new NotSupportedException("Unknown collection type");
            }
        }
        private ITypeSymbol GetElementType(ITypeSymbol collectionType)
        {
            switch (collectionType)
            {
            case INamedTypeSymbol namedType:
                if (namedType.IsGenericType == false)
                {
                    if (ObjectHelper.IsSystemObject(namedType.BaseType))
                    {
                        return(namedType.BaseType);
                    }
                    return(GetElementType(namedType.BaseType));
                }
                return(namedType.TypeArguments[0]);

            case IArrayTypeSymbol arrayType:
                return(arrayType.ElementType);

            default:
                throw new NotSupportedException("Unknown collection type");
            }
        }