1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc.
23 // Carlos Alberto Cortez <calberto.cortez@gmail.com>
27 using System
.Collections
;
28 using System
.ComponentModel
;
29 using System
.Reflection
;
33 using System
.Collections
.Generic
;
35 namespace System
.Windows
.Forms
37 public static class ListBindingHelper
39 public static object GetList (object list
)
41 return GetList (list
, String
.Empty
);
44 public static object GetList (object dataSource
, string dataMember
)
46 if (dataSource
is IListSource
)
47 dataSource
= ((IListSource
) dataSource
).GetList ();
49 if (dataSource
== null)
52 if (dataMember
== null || dataMember
.Length
== 0)
55 if (dataSource
is IEnumerable
) {
56 IEnumerator e
= ((IEnumerable
) dataSource
).GetEnumerator ();
57 if (e
== null || !e
.MoveNext () || e
.Current
== null) {
58 PropertyDescriptorCollection properties
= GetListItemProperties (dataSource
);
59 if (properties
[dataMember
] == null)
60 throw new ArgumentException ("dataMember");
66 dataSource
= e
.Current
;
69 PropertyDescriptor property
= GetProperty (dataSource
, dataMember
);
71 throw new ArgumentException ("dataMember");
73 return property
.GetValue (dataSource
);
76 public static Type
GetListItemType (object list
)
78 return GetListItemType (list
, String
.Empty
);
81 public static Type
GetListItemType (object dataSource
, string dataMember
)
83 if (dataSource
== null)
86 if (dataMember
!= null && dataMember
.Length
> 0) {
87 PropertyDescriptor property
= GetProperty (dataSource
, dataMember
);
89 return typeof (object);
91 return property
.PropertyType
;
94 if (dataSource
is Array
)
95 return dataSource
.GetType ().GetElementType ();
97 // IEnumerable seems to have higher precedence over IList
98 if (dataSource
is IEnumerable
) {
99 IEnumerator enumerator
= ((IEnumerable
) dataSource
).GetEnumerator ();
100 if (enumerator
.MoveNext ())
101 return enumerator
.Current
.GetType ();
103 if (dataSource
is IList
|| dataSource
.GetType () == typeof (IList
<>)) {
104 PropertyInfo property
= GetPropertyByReflection (dataSource
.GetType (), "Item");
105 return property
.PropertyType
;
108 // fallback to object
109 return typeof (object);
112 return dataSource
.GetType ();
115 public static PropertyDescriptorCollection
GetListItemProperties (object list
)
117 return GetListItemProperties (list
, null);
120 public static PropertyDescriptorCollection
GetListItemProperties (object list
, PropertyDescriptor
[] listAccessors
)
122 list
= GetList (list
);
125 return new PropertyDescriptorCollection (null);
127 if (list
is ITypedList
)
128 return ((ITypedList
)list
).GetItemProperties (listAccessors
);
130 if (listAccessors
== null || listAccessors
.Length
== 0) {
131 Type item_type
= GetListItemType (list
);
132 return TypeDescriptor
.GetProperties (item_type
,
133 new Attribute
[] { new BrowsableAttribute (true) }
);
136 // Take into account only the first property
137 Type property_type
= listAccessors
[0].PropertyType
;
138 if (typeof (IList
).IsAssignableFrom (property_type
) ||
139 typeof (IList
<>).IsAssignableFrom (property_type
)) {
141 PropertyInfo property
= GetPropertyByReflection (property_type
, "Item");
142 return TypeDescriptor
.GetProperties (property
.PropertyType
);
145 return new PropertyDescriptorCollection (new PropertyDescriptor
[0]);
148 public static PropertyDescriptorCollection
GetListItemProperties (object dataSource
, string dataMember
,
149 PropertyDescriptor
[] listAccessors
)
151 throw new NotImplementedException ();
154 public static string GetListName (object list
, PropertyDescriptor
[] listAccessors
)
159 Type item_type
= GetListItemType (list
);
160 return item_type
.Name
;
163 static PropertyDescriptor
GetProperty (object obj
, string property_name
)
165 Attribute
[] attrs
= new Attribute
[] { new BrowsableAttribute (true) }
;
167 PropertyDescriptorCollection properties
;
168 if (obj
is ICustomTypeDescriptor
)
169 properties
= ((ICustomTypeDescriptor
)obj
).GetProperties (attrs
);
171 properties
= TypeDescriptor
.GetProperties (obj
.GetType (), attrs
);
173 return properties
[property_name
];
177 // Need to use reflection as we need to bypass the TypeDescriptor.GetProperties () limitations
179 static PropertyInfo
GetPropertyByReflection (Type type
, string property_name
)
181 foreach (PropertyInfo prop
in type
.GetProperties (BindingFlags
.Public
| BindingFlags
.Instance
))
182 if (prop
.Name
== property_name
)