@@ -10,6 +10,7 @@ namespace System.Windows.Forms {
1010 using System . Collections ;
1111 using System . Collections . Generic ;
1212 using System . Reflection ;
13+ using System . Diagnostics . CodeAnalysis ;
1314
1415 /// <include file='doc\ListBindingHelper.uex' path='docs/doc[@for="ListBindingHelper"]/*' />
1516 /// <devdoc>
@@ -99,8 +100,8 @@ public static string GetListName(object list, PropertyDescriptor[] listAccessors
99100 Type type ;
100101 // We always resolve via type in this case (not an instance)
101102 if ( ( null == listAccessors ) || ( listAccessors . Length == 0 ) ) {
102- Type listAsType = list as Type ;
103- if ( listAsType != null ) {
103+ if ( list is Type listAsType )
104+ {
104105 type = listAsType ;
105106 }
106107 else {
@@ -109,8 +110,7 @@ public static string GetListName(object list, PropertyDescriptor[] listAccessors
109110 }
110111 else {
111112 // We don't walk down - always use type name
112- PropertyDescriptor pd = listAccessors [ 0 ] ;
113- type = pd . PropertyType ;
113+ type = listAccessors [ 0 ] . PropertyType ;
114114 }
115115
116116 name = GetListNameFromType ( type ) ;
@@ -145,33 +145,27 @@ public static PropertyDescriptorCollection GetListItemProperties(object list) {
145145 return pdc ;
146146 }
147147
148- /// <include file='doc\ListBindingHelper.uex' path='docs/doc[@for="ListBindingHelper.GetListItemProperties1"]/*' />
149- public static PropertyDescriptorCollection GetListItemProperties ( object list , PropertyDescriptor [ ] listAccessors ) {
150- PropertyDescriptorCollection pdc ;
151-
152- if ( ( null == listAccessors ) || ( listAccessors . Length == 0 ) ) {
153- pdc = GetListItemProperties ( list ) ;
148+ public static PropertyDescriptorCollection GetListItemProperties ( object list , PropertyDescriptor [ ] listAccessors )
149+ {
150+ if ( listAccessors == null || listAccessors . Length == 0 )
151+ {
152+ return GetListItemProperties ( list ) ;
154153 }
155- else {
156- if ( list is Type ) {
157- pdc = GetListItemPropertiesByType ( list as Type , listAccessors ) ;
158- }
159- else {
160- object target = GetList ( list ) ;
161-
162- if ( target is ITypedList ) {
163- pdc = ( target as ITypedList ) . GetItemProperties ( listAccessors ) ;
164- }
165- else if ( target is IEnumerable ) {
166- pdc = GetListItemPropertiesByEnumerable ( target as IEnumerable , listAccessors ) ;
167- }
168- else {
169- pdc = GetListItemPropertiesByInstance ( target , listAccessors , 0 ) ;
170- }
171- }
154+ else if ( list is Type type )
155+ {
156+ return GetListItemPropertiesByType ( type , listAccessors , 0 ) ;
172157 }
173158
174- return pdc ;
159+ object target = GetList ( list ) ;
160+
161+ if ( target is ITypedList typedList ) {
162+ return typedList . GetItemProperties ( listAccessors ) ;
163+ }
164+ else if ( target is IEnumerable enumerable ) {
165+ return GetListItemPropertiesByEnumerable ( enumerable , listAccessors , 0 ) ;
166+ }
167+
168+ return GetListItemPropertiesByInstance ( target , listAccessors , 0 ) ;
175169 }
176170
177171 /// <include file='doc\ListBindingHelper.uex' path='docs/doc[@for="ListBindingHelper.GetListItemProperties2"]/*' />
@@ -241,6 +235,7 @@ public static Type GetListItemType(object list) {
241235 }
242236
243237 // Create an object of the given type. Throw an exception if this fails.
238+ [ ExcludeFromCodeCoverage ]
244239 private static object CreateInstanceOfType ( Type type ) {
245240 object instancedObject = null ;
246241 Exception instanceException = null ;
@@ -320,19 +315,6 @@ private static string GetListNameFromType(Type type) {
320315 return name ;
321316 }
322317
323- private static PropertyDescriptorCollection GetListItemPropertiesByType ( Type type , PropertyDescriptor [ ] listAccessors ) {
324- PropertyDescriptorCollection pdc = null ;
325-
326- if ( ( null == listAccessors ) || ( listAccessors . Length == 0 ) ) {
327- pdc = GetListItemPropertiesByType ( type ) ;
328- }
329- else {
330- pdc = GetListItemPropertiesByType ( type , listAccessors , 0 ) ;
331- }
332-
333- return pdc ;
334- }
335-
336318 private static PropertyDescriptorCollection GetListItemPropertiesByType ( Type type , PropertyDescriptor [ ] listAccessors , int startIndex ) {
337319 PropertyDescriptorCollection pdc = null ;
338320 Type subType = listAccessors [ startIndex ] . PropertyType ;
@@ -410,39 +392,24 @@ private static PropertyDescriptorCollection GetListItemPropertiesByEnumerable(IE
410392 return pdc ;
411393 }
412394
413- private static PropertyDescriptorCollection GetListItemPropertiesByEnumerable ( IEnumerable enumerable , PropertyDescriptor [ ] listAccessors ) {
414- PropertyDescriptorCollection pdc = null ;
415-
416- if ( ( null == listAccessors ) || ( listAccessors . Length == 0 ) ) {
417- pdc = GetListItemPropertiesByEnumerable ( enumerable ) ;
418- }
419- else {
420- ITypedList typedList = enumerable as ITypedList ;
421- if ( typedList != null ) {
422- pdc = typedList . GetItemProperties ( listAccessors ) ;
423- }
424- else {
425- // Walk the tree
426- pdc = GetListItemPropertiesByEnumerable ( enumerable , listAccessors , 0 ) ;
427- }
428- }
429- return pdc ;
430- }
431-
432395 private static Type GetListItemTypeByEnumerable ( IEnumerable iEnumerable ) {
433396 object instance = GetFirstItemByEnumerable ( iEnumerable ) ;
434397 return ( instance != null ) ? instance . GetType ( ) : typeof ( object ) ;
435398 }
436399
437- private static PropertyDescriptorCollection GetListItemPropertiesByInstance ( object target , PropertyDescriptor [ ] listAccessors , int startIndex ) {
400+ private static PropertyDescriptorCollection GetListItemPropertiesByInstance ( object target , PropertyDescriptor [ ] listAccessors , int startIndex )
401+ {
402+ Debug . Assert ( listAccessors != null ) ;
403+
438404 // At this point, things can be simplified because:
439405 // We know target is _not_ a list
440406 // We have an instance
441407
442408 PropertyDescriptorCollection pdc ;
443409
444410 // Get the value of the first listAccessor
445- if ( listAccessors != null && listAccessors . Length > startIndex ) {
411+ if ( listAccessors . Length > startIndex )
412+ {
446413 // Get the value (e.g. given Foo with property Bar, this gets Foo.Bar)
447414 object value = listAccessors [ startIndex ] . GetValue ( target ) ;
448415
@@ -606,7 +573,7 @@ private static PropertyDescriptorCollection GetListItemPropertiesByEnumerable(IE
606573 else {
607574 pdc = TypeDescriptor . GetProperties ( instance , BrowsableAttributeList ) ;
608575
609- if ( ! ( enumerable is IList ) && ( pdc == null || pdc . Count == 0 ) ) {
576+ if ( ! ( enumerable is IList ) && pdc . Count == 0 ) {
610577 pdc = TypeDescriptor . GetProperties ( enumerable , BrowsableAttributeList ) ;
611578 }
612579 }
0 commit comments