public static GetList ( object dataSource, string dataMember ) : object | ||
dataSource | object | |
dataMember | string | |
return | object |
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);
}
ListBindingHelper::GetList ( object list ) : object |
void ResetList() { if (!is_initialized) { return; } IList l; object source = ListBindingHelper.GetList(datasource, datamember); // // If original source is null, then create a new object list // Otherwise, try to infer the list item type // if (datasource == null) { l = new BindingList <object>(); //list_defaulted = true; } else if (source == null) { //Infer type based on datasource and datamember, // where datasource is an empty IEnumerable // and need to find out the datamember type Type property_type = ListBindingHelper.GetListItemProperties(datasource) [datamember].PropertyType; Type t = typeof(BindingList <>).MakeGenericType(new Type [] { property_type }); l = (IList)Activator.CreateInstance(t); } else if (source is IList) { l = (IList)source; } else if (source is IEnumerable) { IList new_list = GetListFromEnumerable((IEnumerable)source); l = new_list == null ? list : new_list; } else if (source is Type) { Type t = typeof(BindingList <>).MakeGenericType(new Type [] { (Type)source }); l = (IList)Activator.CreateInstance(t); } else { Type t = typeof(BindingList <>).MakeGenericType(new Type[] { source.GetType() }); l = (IList)Activator.CreateInstance(t); l.Add(source); } SetList(l); }