8000 Support py list conversion to IReadOnlyList (#100) · QuantConnect/pythonnet@60e9e86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60e9e86

Browse files
authored
Support py list conversion to IReadOnlyList (#100)
1 parent 30f32b9 commit 60e9e86

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/embed_tests/TestConverter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public void ReadOnlyCollection()
7878
Assert.AreEqual(typeof(int), ((IReadOnlyCollection<Type>) result).ToList()[1]);
7979
}
8080

81+
[Test]
82+
public void ReadOnlyList()
83+
{
84+
var array = new List<Type> { typeof(decimal), typeof(int) };
85+
var py = array.ToPython();
86+
object result;
87+
var converted = Converter.ToManaged(py, typeof(IReadOnlyList<Type>), out result, false);
88+
89+
Assert.IsTrue(converted);
90+
Assert.AreEqual(typeof(List<Type>), result.GetType());
91+
Assert.AreEqual(2, ((IReadOnlyList<Type>)result).Count);
92+
Assert.AreEqual(typeof(decimal), ((IReadOnlyList<Type>)result).ToList()[0]);
93+
Assert.AreEqual(typeof(int), ((IReadOnlyList<Type>)result).ToList()[1]);
94+
}
95+
8196
[Test]
8297
public void ConvertPyListToArray()
8398
{

src/runtime/Converter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
421421
if (typeDefinition == typeof(List<>)
422422
|| typeDefinition == typeof(IList<>)
423423
|| typeDefinition == typeof(IEnumerable<>)
424-
|| typeDefinition == typeof(IReadOnlyCollection<>))
424+
|| typeDefinition == typeof(IReadOnlyCollection<>)
425+
|| typeDefinition == typeof(IReadOnlyList<>))
425426
{
426427
return ToList(value, obType, out result, setError);
427428
}

0 commit comments

Comments
 (0)
0