Skip to content

Commit 2ee3513

Browse files
Fix SetSnapShot CopyTo variance failure
1 parent 79ad5b8 commit 2ee3513

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/NHibernate.Test/UtilityTest/SetSnapShotFixture.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Runtime.Serialization.Formatters.Binary;
45
using NHibernate.Collection.Generic.SetHelpers;
@@ -70,6 +71,17 @@ public void TestCopyTo()
7071
Assert.That(list, Is.EquivalentTo(array));
7172
}
7273

74+
[Test]
75+
public void TestCopyToObjectArray()
76+
{
77+
var list = new List<string> { "test1", null, "test2" };
78+
ICollection sn = new SetSnapShot<string>(list);
79+
80+
var array = new object[3];
81+
sn.CopyTo(array, 0);
82+
Assert.That(list, Is.EquivalentTo(array));
83+
}
84+
7385
[Test]
7486
public void TestSerialization()
7587
{

src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,22 @@ IEnumerator IEnumerable.GetEnumerator()
114114

115115
void ICollection.CopyTo(Array array, int index)
116116
{
117-
if (!(array is T[] typedArray))
117+
if (array is T[] typedArray)
118118
{
119-
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
119+
CopyTo(typedArray, index);
120+
return;
120121
}
121122

122-
CopyTo(typedArray, index);
123+
if (array.GetType().GetElementType().IsAssignableFrom(typeof(T)))
124+
{
125+
if (_hasNull)
126+
array.SetValue(default(T), index);
127+
ICollection keysCollection = _values.Keys;
128+
keysCollection.CopyTo(array, index + (_hasNull ? 1 : 0));
129+
return;
130+
}
131+
132+
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
123133
}
124134

125135
public int Count => _values.Count + (_hasNull ? 1 : 0);

0 commit comments

Comments
 (0)