Skip to content

Commit 51a5c43

Browse files
authored
Bring back IList conformance to RealmCollectionBase (#3150)
1 parent 7841c39 commit 51a5c43

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Slightly improve performance of `Realm.RemoveAll()` which removes all objects from an open Realm database. (Issue [#2233](https://github.com/realm/realm-dotnet/issues/2194))
1616
* Improve error messages when not setting a BaseFilePath for realm or app configuration. (Issue [2863](https://github.com/realm/realm-dotnet/issues/2863))
1717
* Added diagnostic error for nested classes used with the source generator syntax, as they are not yet supported. (Issue [#3130](https://github.com/realm/realm-dotnet/issues/3130))
18+
* Added `IList` implementation to all Realm collections to allow for UWP ListView databinding. (Issue [#1759](https://github.com/realm/realm-dotnet/issues/1759))
1819

1920
### Fixed
2021
* Fixed issue where Realm parameters' initialization would get run twice, resulting in unexpected behavior.

Realm/Realm/DatabaseTypes/RealmCollectionBase.cs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ namespace Realms
3535
{
3636
[EditorBrowsable(EditorBrowsableState.Never)]
3737
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "This should not be directly accessed by users.")]
38+
[SuppressMessage("Design", "CA1010:Generic interface should also be implemented", Justification = "IList conformance is needed for UWP databinding. IList<T> is not necessary.")]
3839
public abstract class RealmCollectionBase<T>
3940
: INotifiable<NotifiableObjectHandleBase.CollectionChangeSet>,
4041
IRealmCollection<T>,
42+
IList,
4143
IThreadConfined,
4244
IMetadataObject
4345
{
@@ -452,11 +454,19 @@ private void UpdateCollectionChangedSubscriptionIfNecessary(bool isSubscribed)
452454

453455
public virtual bool IsReadOnly => (Realm?.Config as RealmConfiguration)?.IsReadOnly == true;
454456

457+
public bool IsFixedSize => false;
458+
459+
public bool IsSynchronized => false;
460+
461+
public object SyncRoot => null;
462+
463+
object IList.this[int index] { get => this[index]; set => throw new NotSupportedException(); }
464+
455465
public void Clear() => Handle.Value.Clear();
456466

457467
public int IndexOf(object value)
458468
{
459-
if (value != null && !(value is T))
469+
if (value != null && value is not T)
460470
{
461471
throw new ArgumentException($"value must be of type {typeof(T).FullName}, but got {value?.GetType().FullName}", nameof(value));
462472
}
@@ -466,7 +476,7 @@ public int IndexOf(object value)
466476

467477
public bool Contains(object value)
468478
{
469-
if (value != null && !(value is T))
479+
if (value != null && value is not T)
470480
{
471481
throw new ArgumentException($"value must be of type {typeof(T).FullName}, but got {value?.GetType().FullName}", nameof(value));
472482
}
@@ -498,6 +508,59 @@ public void CopyTo(T[] array, int arrayIndex)
498508
}
499509
}
500510

511+
public virtual int Add(object value)
512+
{
513+
if (value is T tValue)
514+
{
515+
Add(tValue);
516+
return Count - 1;
517+
}
518+
519+
throw new NotSupportedException($"Can't add an item of type {value?.GetType()?.Name ?? "null"} to a list of {typeof(T).Name}");
520+
}
521+
522+
public virtual void Insert(int index, object value)
523+
{
524+
if (value is T tValue)
525+
{
526+
Insert(index, tValue);
527+
}
528+
else
529+
{
530+
throw new NotSupportedException($"Can't add an item of type {value?.GetType()?.Name ?? "null"} to a list of {typeof(T).Name}");
531+
}
532+
}
533+
534+
public void Remove(object value)
535+
{
536+
if (value is T tValue)
537+
{
538+
Remove(tValue);
539+
}
540+
}
541+
542+
public virtual void RemoveAt(int index) => throw new NotSupportedException();
543+
544+
public void CopyTo(Array array, int index)
545+
{
546+
Argument.NotNull(array, nameof(array));
547+
548+
if (index < 0)
549+
{
550+
throw new ArgumentOutOfRangeException(nameof(index));
551+
}
552+
553+
if (index + Count > array.Length)
554+
{
555+
throw new ArgumentException($"Specified array doesn't have enough capacity to perform the copy. Needed: {index + Count}, available: {array.Length}", nameof(array));
556+
}
557+
558+
foreach (var obj in this)
559+
{
560+
array.SetValue(obj, index++);
561+
}
562+
}
563+
501564
#endregion IList
502565

503566
public class Enumerator : IEnumerator<T>

Realm/Realm/DatabaseTypes/RealmList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public bool Remove(T item)
155155
return true;
156156
}
157157

158-
public void RemoveAt(int index)
158+
public override void RemoveAt(int index)
159159
{
160160
if (index < 0)
161161
{

0 commit comments

Comments
 (0)