public bool TryGetDescriptor(Type type, out ObjectDescriptor value)
        {
            if (!(type != StringType && Type.IsAssignableFrom(type)))
            {
                value = ObjectDescriptor.Empty;
                return(false);
            }

            if (!_descriptorProvider.TryGetDescriptor(type, out value))
            {
                value = ObjectDescriptor.Empty;
                return(false);
            }

            var enumerableMemberAccessor = EnumerableMemberAccessor.Create(type);
            var mergedMemberAccessor     = new MergedMemberAccessor(enumerableMemberAccessor, value.MemberAccessor);

            var parameters = new object[] { mergedMemberAccessor, value };

            return(TryCreateArrayDescriptor(type, parameters, out value) ||
                   TryCreateDescriptorFromOpenGeneric(type, typeof(IList <>), parameters, ListObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptorFromOpenGeneric(type, typeof(IReadOnlyList <>), parameters, ReadOnlyListObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptorFromOpenGeneric(type, typeof(ICollection <>), parameters, CollectionObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptorFromOpenGeneric(type, typeof(IReadOnlyCollection <>), parameters, ReadOnlyCollectionObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptorFromOpenGeneric(type, typeof(IEnumerable <>), parameters, EnumerableObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptor(type, typeof(IList), parameters, NonGenericListObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptor(type, typeof(ICollection), parameters, NonGenericCollectionObjectDescriptorFactoryMethodInfo, out value) ||
                   TryCreateDescriptor(type, typeof(IEnumerable), parameters, NonGenericEnumerableObjectDescriptorFactoryMethodInfo, out value));
        }
Пример #2
0
        public bool TryGetDescriptor(Type type, out ObjectDescriptor value)
        {
            if (!(type != StringType && Type.IsAssignableFrom(type)))
            {
                value = ObjectDescriptor.Empty;
                return(false);
            }

            if (!_descriptorProvider.TryGetDescriptor(type, out value))
            {
                value = ObjectDescriptor.Empty;
                return(false);
            }

            var enumerableMemberAccessor = EnumerableMemberAccessor.Create(type);
            var mergedMemberAccessor     = new MergedMemberAccessor(enumerableMemberAccessor, value.MemberAccessor);

            value = new ObjectDescriptor(value.DescribedType, mergedMemberAccessor, value.GetProperties, true);

            return(true);
        }