File tree Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections ;
3
+ using System . Collections . Generic ;
3
4
using System . Diagnostics ;
4
5
using System . Runtime . InteropServices ;
5
6
using System . Runtime . Serialization ;
@@ -184,7 +185,6 @@ public static IntPtr tp_iter(IntPtr ob)
184
185
185
186
var e = co . inst as IEnumerable ;
186
187
IEnumerator o ;
187
-
188
188
if ( e != null )
189
189
{
190
190
o = e . GetEnumerator ( ) ;
@@ -199,7 +199,22 @@ public static IntPtr tp_iter(IntPtr ob)
199
199
}
200
200
}
201
201
202
- return new Iterator ( o ) . pyHandle ;
202
+ var elemType = typeof ( object ) ;
203
+ var iterType = co . inst . GetType ( ) ;
204
+ foreach ( var ifc in iterType . GetInterfaces ( ) )
205
+ {
206
+ if ( ifc . IsGenericType )
207
+ {
208
+ var genTypeDef = ifc . GetGenericTypeDefinition ( ) ;
209
+ if ( genTypeDef == typeof ( IEnumerable < > ) || genTypeDef == typeof ( IEnumerator < > ) )
210
+ {
211
+ elemType = ifc . GetGenericArguments ( ) [ 0 ] ;
212
+ break ;
213
+ }
214
+ }
215
+ }
216
+
217
+ return new Iterator ( o , elemType ) . pyHandle ;
203
218
}
204
219
205
220
Original file line number Diff line number Diff line change @@ -10,10 +10,12 @@ namespace Python.Runtime
10
10
internal class Iterator : ExtensionType
11
11
{
12
12
private IEnumerator iter ;
13
+ private Type elemType ;
13
14
14
- public Iterator ( IEnumerator e )
15
+ public Iterator ( IEnumerator e , Type elemType )
15
16
{
16
17
iter = e ;
18
+ this . elemType = elemType ;
17
19
}
18
20
19
21
@@ -41,7 +43,7 @@ public static IntPtr tp_iternext(IntPtr ob)
41
43
return IntPtr . Zero ;
42
44
}
43
45
object item = self . iter . Current ;
44
- return Converter . ToPythonImplicit ( item ) ;
46
+ return Converter . ToPython ( item , self . elemType ) ;
45
47
}
46
48
47
49
public static IntPtr tp_iter ( IntPtr ob )
Original file line number Diff line number Diff line change @@ -120,3 +120,19 @@ def test_implementation_access():
120
120
assert 100 == i .__implementation__
121
121
assert clrVal == i .__raw_implementation__
122
122
assert i .__implementation__ != i .__raw_implementation__
123
+
124
+
125
+ def test_interface_collection_iteration ():
126
+ """Test interface type is used when iterating over interface collection"""
127
+ import System
128
+ from System .Collections .Generic import List
129
+ elem = System .IComparable (System .Int32 (100 ))
130
+ typed_list = List [System .IComparable ]()
131
+ typed_list .Add (elem )
132
+ for e in typed_list :
133
+ assert type (e ).__name__ == "IComparable"
134
+
135
+ untyped_list = System .Collections .ArrayList ()
136
+ untyped_list .Add (elem )
137
+ for e in untyped_list :
138
+ assert type (e ).__name__ == "int"
You can’t perform that action at this time.
0 commit comments