Skip to content

Commit 2895616

Browse files
committed
Revert to initial constructor and try to instantiate TSource using reflection
Tested the original constructor with TSource (with / without default constructor)
1 parent 900fcc2 commit 2895616

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Microsoft.Toolkit.Uwp/IncrementalLoadingCollection/IncrementalLoadingCollection.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.ObjectModel;
1616
using System.ComponentModel;
1717
using System.Linq;
18+
using System.Reflection;
1819
using System.Runtime.InteropServices.WindowsRuntime;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -117,6 +118,27 @@ private set
117118
}
118119
}
119120

121+
/// <summary>
122+
/// Initializes a new instance of the <see cref="IncrementalLoadingCollection{TSource, IType}"/> class optionally specifying how many items to load for each data page.
123+
/// </summary>
124+
/// <param name="itemsPerPage">
125+
/// The number of items to retrieve for each call. Default is 20.
126+
/// </param>
127+
/// <param name="onStartLoading">
128+
/// An <see cref="Action"/> that is called when a retrieval operation begins.
129+
/// </param>
130+
/// <param name="onEndLoading">
131+
/// An <see cref="Action"/> that is called when a retrieval operation ends.
132+
/// </param>
133+
/// <param name="onError">
134+
/// An <see cref="Action"/> that is called if an error occours during data retrieval.
135+
/// </param>
136+
/// <seealso cref="IIncrementalSource{TSource}"/>
137+
public IncrementalLoadingCollection(int itemsPerPage = 20, Action onStartLoading = null, Action onEndLoading = null, Action<Exception> onError = null)
138+
: this(default(TSource), itemsPerPage, onStartLoading, onEndLoading, onError)
139+
{
140+
}
141+
120142
/// <summary>
121143
/// Initializes a new instance of the <see cref="IncrementalLoadingCollection{TSource, IType}"/> class using the specified <see cref="IIncrementalSource{TSource}"/> implementation and, optionally, how many items to load for each data page.
122144
/// </summary>
@@ -138,6 +160,18 @@ private set
138160
/// <seealso cref="IIncrementalSource{TSource}"/>
139161
public IncrementalLoadingCollection(TSource source, int itemsPerPage = 20, Action onStartLoading = null, Action onEndLoading = null, Action<Exception> onError = null)
140162
{
163+
if (EqualityComparer<TSource>.Default.Equals(source, default(TSource)))
164+
{
165+
var type = typeof(TSource);
166+
ConstructorInfo constructor = type.GetConstructor(new Type[0]);
167+
if (constructor == null)
168+
{
169+
throw new InvalidOperationException("TSource must have a parameterless constructor");
170+
}
171+
172+
source = (TSource)constructor.Invoke(null);
173+
}
174+
141175
Source = source;
142176

143177
_onStartLoading = onStartLoading;

0 commit comments

Comments
 (0)