Skip to content

Commit 888da65

Browse files
hughbeZachary Danz
authored andcommitted
Remove dead code from binding (#393)
1 parent a0f5046 commit 888da65

2 files changed

Lines changed: 55 additions & 89 deletions

File tree

src/System.Windows.Forms/src/System/Windows/Forms/BindingSource.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace System.Windows.Forms {
1818
using System.Collections.Generic;
1919
using System.ComponentModel;
2020
using System.Diagnostics;
21+
using System.Diagnostics.CodeAnalysis;
2122
using System.Drawing;
2223
using System.Globalization;
2324
using System.Reflection;
@@ -58,7 +59,7 @@ public class BindingSource : Component,
5859
private string dataMember = string.Empty;
5960
private string sort = null;
6061
private string filter = null;
61-
private CurrencyManager currencyManager;
62+
private readonly CurrencyManager currencyManager;
6263
private bool raiseListChangedEvents = true;
6364
private bool parentsCurrentItemChanging = false;
6465
private bool disposedOrFinalized = false;
@@ -1412,23 +1413,23 @@ private void UnhookItemChangedEventsForOldCurrent() {
14121413
///////////////////////////////////////////////////////////////////////////////
14131414

14141415
private void WireCurrencyManager(CurrencyManager cm) {
1415-
if (cm != null) {
1416-
cm.PositionChanged += new EventHandler(CurrencyManager_PositionChanged);
1417-
cm.CurrentChanged += new EventHandler(CurrencyManager_CurrentChanged);
1418-
cm.CurrentItemChanged += new EventHandler(CurrencyManager_CurrentItemChanged);
1419-
cm.BindingComplete += new BindingCompleteEventHandler(CurrencyManager_BindingComplete);
1420-
cm.DataError += new BindingManagerDataErrorEventHandler(CurrencyManager_DataError);
1421-
}
1416+
Debug.Assert(cm != null);
1417+
1418+
cm.PositionChanged += new EventHandler(CurrencyManager_PositionChanged);
1419+
cm.CurrentChanged += new EventHandler(CurrencyManager_CurrentChanged);
1420+
cm.CurrentItemChanged += new EventHandler(CurrencyManager_CurrentItemChanged);
1421+
cm.BindingComplete += new BindingCompleteEventHandler(CurrencyManager_BindingComplete);
1422+
cm.DataError += new BindingManagerDataErrorEventHandler(CurrencyManager_DataError);
14221423
}
14231424

14241425
private void UnwireCurrencyManager(CurrencyManager cm) {
1425-
if (cm != null) {
1426-
cm.PositionChanged -= new EventHandler(CurrencyManager_PositionChanged);
1427-
cm.CurrentChanged -= new EventHandler(CurrencyManager_CurrentChanged);
1428-
cm.CurrentItemChanged -= new EventHandler(CurrencyManager_CurrentItemChanged);
1429-
cm.BindingComplete -= new BindingCompleteEventHandler(CurrencyManager_BindingComplete);
1430-
cm.DataError -= new BindingManagerDataErrorEventHandler(CurrencyManager_DataError);
1431-
}
1426+
Debug.Assert(cm != null);
1427+
1428+
cm.PositionChanged -= new EventHandler(CurrencyManager_PositionChanged);
1429+
cm.CurrentChanged -= new EventHandler(CurrencyManager_CurrentChanged);
1430+
cm.CurrentItemChanged -= new EventHandler(CurrencyManager_CurrentItemChanged);
1431+
cm.BindingComplete -= new BindingCompleteEventHandler(CurrencyManager_BindingComplete);
1432+
cm.DataError -= new BindingManagerDataErrorEventHandler(CurrencyManager_DataError);
14321433
}
14331434

14341435
private void WireDataSource() {
@@ -1514,17 +1515,15 @@ void ISupportInitialize.EndInit() {
15141515
}
15151516
}
15161517

1517-
// Respond to late completion of the DataSource's initialization, by completing our own initialization.
1518-
// This situation can arise if the call to the DataSource's EndInit() method comes after the call to the
1519-
// BindingSource's EndInit() method (since code-generated ordering of these calls is non-deterministic).
1520-
//
1521-
private void DataSource_Initialized(object sender, EventArgs e) {
1522-
ISupportInitializeNotification dsInit = (this.DataSource as ISupportInitializeNotification);
1523-
1524-
Debug.Assert(dsInit != null, "BindingSource: ISupportInitializeNotification.Initialized event received, but current DataSource does not support ISupportInitializeNotification!");
1525-
Debug.Assert(dsInit.IsInitialized, "BindingSource: DataSource sent ISupportInitializeNotification.Initialized event but before it had finished initializing.");
1526-
1527-
if (dsInit != null) {
1518+
/// <devdoc>
1519+
/// Respond to late completion of the DataSource's initialization, by completing our own initialization.
1520+
/// This situation can arise if the call to the DataSource's EndInit() method comes after the call to the
1521+
/// BindingSource's EndInit() method (since code-generated ordering of these calls is non-deterministic).
1522+
/// </devdoc>
1523+
private void DataSource_Initialized(object sender, EventArgs e)
1524+
{
1525+
if (DataSource is ISupportInitializeNotification dsInit)
1526+
{
15281527
dsInit.Initialized -= new EventHandler(DataSource_Initialized);
15291528
}
15301529

src/System.Windows.Forms/src/System/Windows/Forms/ListBindingHelper.cs

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)