public static object GetList(object dataSource, string dataMember) {
//
// The purpose of this method is to find a list, given a 'data source' object and a
// decription of some 'data member' property of that object which returns the list.
//
// - If the data source is not a list, we get the list by just querying for the
// current value of that property on the data source itself.
//
// - If the data source is a list, we have to first pick some item from that list,
// then query for the value of that property on the individual list item.
//
dataSource = GetList(dataSource);
if (dataSource == null || dataSource is Type || String.IsNullOrEmpty(dataMember)) {
return dataSource;
}
PropertyDescriptorCollection dsProps = ListBindingHelper.GetListItemProperties(dataSource);
PropertyDescriptor dmProp = dsProps.Find(dataMember, true);
if (dmProp == null) {
throw new System.ArgumentException(SR.GetString(SR.DataSourceDataMemberPropNotFound, dataMember));
}
object currentItem;
if (dataSource is ICurrencyManagerProvider) {
// Data source is another BindingSource so ask for its current item
CurrencyManager cm = (dataSource as ICurrencyManagerProvider).CurrencyManager;
bool currentKnown = (cm != null && cm.Position >= 0 && cm.Position <= cm.Count - 1);
currentItem = currentKnown ? cm.Current : null;
}
else if (dataSource is IEnumerable) {
// Data source is an enumerable list, so walk to the first item
currentItem = GetFirstItemByEnumerable(dataSource as IEnumerable);
}
else {
// Data source is not a list, so just use the data source itself
currentItem = dataSource;
}
// Query the data member property on the chosen object to get back the list
return (currentItem == null) ? null : dmProp.GetValue(currentItem);
}