Beispiel #1
0
        public Indexer Close([NotNull] Type[] typeClosures)
        {
            if (typeClosures == null)
            {
                throw new ArgumentNullException("typeClosures");
            }

            // Check input arrays are valid.
            if (typeClosures.Length != ExtendedType.GenericArguments.Count())
            {
                return(null);
            }

            // If we haven't got any type closures, we can return this indexer.
            if (typeClosures.All(t => t == null))
            {
                return(this);
            }

            // Close type
            ExtendedType et = ExtendedType.CloseType(typeClosures);

            // Check closure succeeded.
            if (et == null)
            {
                return(null);
            }

            // Create new search.
            Debug.Assert(_indexParameters.Value != null);
            int pCount = _indexParameters.Value.Length;

            TypeSearch[] searchTypes = new TypeSearch[pCount + 1];

            Type[] typeGenericArguments = et.GenericArguments.Select(g => g.Type).ToArray();

            // Search for closed
            Type[] emptyTypes = Array <Type> .Empty;
            for (int i = 0; i < pCount; i++)
            {
                Debug.Assert(_indexParameters.Value[i] != null);
                Type pType = _indexParameters.Value[i].ParameterType;
                searchTypes[i] = Reflection.ExpandParameterType(pType, emptyTypes, typeGenericArguments);
            }

            // Add return type
            searchTypes[pCount] = Reflection.ExpandParameterType(
                Info.PropertyType,
                emptyTypes,
                typeGenericArguments);

            // Search for indexer on new type.
            return(et.GetIndexer(searchTypes));
        }