diff --git a/src/embed_tests/dynamic.cs b/src/embed_tests/dynamic.cs index 827782a5c..0a181231c 100644 --- a/src/embed_tests/dynamic.cs +++ b/src/embed_tests/dynamic.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text; using NUnit.Framework; using Python.Runtime; @@ -126,5 +127,15 @@ public void PassPyObjectInNet() // Compare in .NET Assert.IsTrue(sys.testattr1.Equals(sys.testattr2)); } + + // regression test for https://github.com/pythonnet/pythonnet/issues/1680 + [Test] + public void ForEach() + { + dynamic pyList = PythonEngine.Eval("[1,2,3]"); + var list = new List(); + foreach (int item in pyList) + list.Add(item); + } } } diff --git a/src/runtime/PythonTypes/PyObject.cs b/src/runtime/PythonTypes/PyObject.cs index 373751cf6..e26831490 100644 --- a/src/runtime/PythonTypes/PyObject.cs +++ b/src/runtime/PythonTypes/PyObject.cs @@ -1296,6 +1296,12 @@ public override bool TryConvert(ConvertBinder binder, out object? result) return converted; } + if (binder.Type == typeof(System.Collections.IEnumerable) && this.IsIterable()) + { + result = new PyIterable(this.Reference); + return true; + } + return false; }