Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return interface objects when iterating over interface collections#1257

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 1 commit intopythonnet:masterfromdanabr:interface-collections
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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
19 changes: 17 additions & 2 deletionssrc/runtime/classbase.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
Expand DownExpand Up@@ -184,7 +185,6 @@ public static IntPtr tp_iter(IntPtr ob)

var e = co.inst as IEnumerable;
IEnumerator o;

if (e != null)
{
o = e.GetEnumerator();
Expand All@@ -199,7 +199,22 @@ public static IntPtr tp_iter(IntPtr ob)
}
}

return new Iterator(o).pyHandle;
var elemType = typeof(object);
var iterType = co.inst.GetType();
foreach(var ifc in iterType.GetInterfaces())
{
if (ifc.IsGenericType)
{
var genTypeDef = ifc.GetGenericTypeDefinition();
if (genTypeDef == typeof(IEnumerable<>) || genTypeDef == typeof(IEnumerator<>))
{
elemType = ifc.GetGenericArguments()[0];
break;
}
}
}

return new Iterator(o, elemType).pyHandle;
}


Expand Down
6 changes: 4 additions & 2 deletionssrc/runtime/iterator.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,10 +10,12 @@ namespace Python.Runtime
internal class Iterator : ExtensionType
{
private IEnumerator iter;
private Type elemType;

public Iterator(IEnumerator e)
public Iterator(IEnumerator e, Type elemType)
{
iter = e;
this.elemType = elemType;
}


Expand DownExpand Up@@ -41,7 +43,7 @@ public static IntPtr tp_iternext(IntPtr ob)
return IntPtr.Zero;
}
object item = self.iter.Current;
return Converter.ToPythonImplicit(item);
return Converter.ToPython(item, self.elemType);
}

public static IntPtr tp_iter(IntPtr ob)
Expand Down
16 changes: 16 additions & 0 deletionssrc/tests/test_interface.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -120,3 +120,19 @@ def test_implementation_access():
assert 100 == i.__implementation__
assert clrVal == i.__raw_implementation__
assert i.__implementation__ != i.__raw_implementation__


def test_interface_collection_iteration():
"""Test interface type is used when iterating over interface collection"""
import System
from System.Collections.Generic import List
elem = System.IComparable(System.Int32(100))
typed_list = List[System.IComparable]()
typed_list.Add(elem)
for e in typed_list:
assert type(e).__name__ == "IComparable"

untyped_list = System.Collections.ArrayList()
untyped_list.Add(elem)
for e in untyped_list:
assert type(e).__name__ == "int"

[8]ページ先頭

©2009-2025 Movatter.jp