Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support py list conversion to IReadOnlyList #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public void ReadOnlyCollection()
Assert.AreEqual(typeof(int), ((IReadOnlyCollection<Type>) result).ToList()[1]);
}

[Test]
public void ReadOnlyList()
{
var array = new List<Type> { typeof(decimal), typeof(int) };
var py = array.ToPython();
object result;
var converted = Converter.ToManaged(py, typeof(IReadOnlyList<Type>), out result, false);

Assert.IsTrue(converted);
Assert.AreEqual(typeof(List<Type>), result.GetType());
Assert.AreEqual(2, ((IReadOnlyList<Type>)result).Count);
Assert.AreEqual(typeof(decimal), ((IReadOnlyList<Type>)result).ToList()[0]);
Assert.AreEqual(typeof(int), ((IReadOnlyList<Type>)result).ToList()[1]);
}

[Test]
public void ConvertPyListToArray()
{
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,
if (typeDefinition == typeof(List<>)
|| typeDefinition == typeof(IList<>)
|| typeDefinition == typeof(IEnumerable<>)
|| typeDefinition == typeof(IReadOnlyCollection<>))
|| typeDefinition == typeof(IReadOnlyCollection<>)
|| typeDefinition == typeof(IReadOnlyList<>))
{
return ToList(value, obType, out result, setError);
}
Expand Down
Loading