Skip to content

Commit 8f44b12

Browse files
kzrnmpunker76
authored andcommitted
Check subclass of ObservableCollection<>
1 parent c148ea1 commit 8f44b12

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/GongSolutions.WPF.DragDrop/Utilities/TypeUtilities.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Collections;
66
using System.Collections.ObjectModel;
7+
using JetBrains.Annotations;
78

89
namespace GongSolutions.Wpf.DragDrop.Utilities
910
{
@@ -86,11 +87,24 @@ public static IList TryGetList(this IEnumerable enumerable)
8687
/// </summary>
8788
/// <param name="collection">The collection to test.</param>
8889
/// <returns>True if the collection is a ObservableCollection&lt;&gt;</returns>
89-
public static bool IsObservableCollection(this IList collection)
90+
public static bool IsObservableCollection([CanBeNull] this IList collection)
9091
{
91-
return collection != null
92-
&& collection.GetType().IsGenericType
93-
&& collection.GetType().GetGenericTypeDefinition() == typeof(ObservableCollection<>);
92+
return collection != null && IsObservableCollectionType(collection.GetType());
93+
}
94+
95+
private static bool IsObservableCollectionType([CanBeNull] Type type)
96+
{
97+
if (type is null || !typeof(IList).IsAssignableFrom(type))
98+
{
99+
return false;
100+
}
101+
102+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
103+
{
104+
return true;
105+
}
106+
107+
return IsObservableCollectionType(type.BaseType);
94108
}
95109

96110
/// <summary>

src/Showcase/Models/ItemModel.cs

+4
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
158158
}
159159
}
160160

161+
public class ItemModelObservableCollection : ObservableCollection<ItemModel>
162+
{
163+
}
164+
161165
[Serializable]
162166
public class SerializableItemModel
163167
{

src/Showcase/Models/SampleData.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public SampleData()
1717
this.ClonableCollection1.Add(new ClonableItemModel(n + 1));
1818
this.DataGridCollection1.Add(new DataGridRowModel());
1919
}
20+
2021
for (var n = 0; n < 10; ++n)
2122
{
2223
this.Collection4.Add(new ItemModel() { Caption = $"Model {n + 1}" });
@@ -40,6 +41,7 @@ public SampleData()
4041
{
4142
root.Children.Add(new TreeNode($"Item {i + 10 * r}"));
4243
}
44+
4345
this.TreeCollection1.Add(root);
4446
if (r == 2)
4547
{
@@ -51,6 +53,7 @@ public SampleData()
5153
{
5254
this.TabItemCollection1.Add(new TabItemModel(i + 1));
5355
}
56+
5457
this.TabItemCollection2.Add(new TabItemModel(1));
5558
}
5659

@@ -62,7 +65,7 @@ public SampleData()
6265

6366
public SerializableDropHandler SerializableDropHandler { get; set; } = new SerializableDropHandler();
6467

65-
public ObservableCollection<ItemModel> Collection1 { get; set; } = new ObservableCollection<ItemModel>();
68+
public ItemModelObservableCollection Collection1 { get; set; } = new ItemModelObservableCollection();
6669

6770
public ObservableCollection<ItemModel> Collection2 { get; set; } = new ObservableCollection<ItemModel>();
6871

0 commit comments

Comments
 (0)