System.Windows.Forms.ListBindingHelper.GetListItemType C# (CSharp) Method

GetListItemType() public static method

public static GetListItemType ( object list ) : Type
list object
return Type
        public static Type GetListItemType(object list) {
            if (list == null) {
                return null;
            }

            Type itemType = null;

            // special case for IListSource
            if ((list is Type) && (typeof(IListSource).IsAssignableFrom(list as Type))) {
                list = CreateInstanceOfType(list as Type);
            }

            list = GetList(list);
            Type listType = (list is Type) ? (list as Type) : list.GetType();
            object listInstance = (list is Type) ? null : list;

            if (typeof(Array).IsAssignableFrom(listType)) {
                itemType = listType.GetElementType();
            }
            else {
                PropertyInfo indexer = GetTypedIndexer(listType);

                if (indexer != null) {
                    itemType = indexer.PropertyType;
                }
                else if (listInstance is IEnumerable) {
                    itemType = GetListItemTypeByEnumerable(listInstance as IEnumerable);
                }
                else {
                    itemType = listType;
                }
            }

            return itemType;
        }

Same methods

ListBindingHelper::GetListItemType ( object dataSource, string dataMember ) : Type

Usage Example

Example #1
0
        void SetList(IList l)
        {
            if (list is IBindingList)
            {
                ((IBindingList)list).ListChanged -= IBindingListChangedHandler;
            }

            list                  = l;
            item_type             = ListBindingHelper.GetListItemType(list);
            item_has_default_ctor = item_type.GetConstructor(Type.EmptyTypes) != null;

            list_is_ibinding = list is IBindingList;
            if (list_is_ibinding)
            {
                ((IBindingList)list).ListChanged += IBindingListChangedHandler;

                if (list is IBindingListView)
                {
                    ((IBindingListView)list).Filter = filter;
                }
            }

            ResetBindings(true);
        }