Microsoft.CSharp.RuntimeBinder.RuntimeBinderException occurred
HResult=0x80131500
Message=Cannot implicitly convert type 'Python.Runtime.PyObject' to 'System.Collections.Generic.List<string>'
Source=Python.EmbeddingTest
StackTrace:
at Python.EmbeddingTest.PyImportTest.TestGenericListMarshalling() in D:\Users\Tom\Dropbox\Algo Trading\pythonnet\src\embed_tests\pyimport.cs:line 90
I pulled latest from master but I'm afraid I still get the problem. It looks like casting to a string[] array is successful, but the C# base class (and interface) are defined with a List<string> so I was expecting to be able to cast the dynamic back to a List<string>..
Were you able to reproduce the issue? What I did was I added the Interface1.cs, Class1.cs, Module.py to the Python.EmbeddingTest project. (By the way, in that project, any reason the 'fixtures' folder is not included in the project file?)
@tomhunter-gh can you share your full project in a zip file? just paste it here to this issue.
fixtures are not included because it is python code, but could be included as a resource. we have a separate pyproj in the directory with python tests.
@tomhunter-gh actually that pull request which I linked above #427 leads to this issue :(
Here is the code that I commented out to pass your the test:
/*if (value is IList && value.GetType().IsGenericType)
{
using (var resultlist = new PyList())
{
foreach (object o in (IEnumerable)value)
{
using (var p = new PyObject(ToPython(o, o?.GetType())))
{
resultlist.Append(p);
}
}
Runtime.XIncref(resultlist.Handle);
return resultlist.Handle;
}
}*/
The reason is because before the list is passed back to C#, it gets converted to Python list.
So quick workaround would be to provide equivalent conversion from Python to C#: list -> List.
But this is of course a lot of conversion back and forth and maybe a performance issue.
Activity
den-run-ai commented on Apr 5, 2017
@tomhunter-gh can you try from the master?
pip install git+https://github.com/pythonnet/pythonnet -U --force
This should have been fixed recently:
#427
tomhunter-gh commented on Apr 6, 2017
Hi @denfromufa
I pulled latest from master but I'm afraid I still get the problem. It looks like casting to a string[] array is successful, but the C# base class (and interface) are defined with a List<string> so I was expecting to be able to cast the dynamic back to a List<string>..
Were you able to reproduce the issue? What I did was I added the Interface1.cs, Class1.cs, Module.py to the Python.EmbeddingTest project. (By the way, in that project, any reason the 'fixtures' folder is not included in the project file?)
Thanks
den-run-ai commented on Apr 6, 2017
@tomhunter-gh can you share your full project in a zip file? just paste it here to this issue.
fixtures are not included because it is python code, but could be included as a resource. we have a separate pyproj in the directory with python tests.
tomhunter-gh commented on Apr 6, 2017
https://www.dropbox.com/s/0u4rbu5s2lr24b0/pythonnet.zip?dl=0
tomhunter-gh commented on Apr 6, 2017
By the way I'm using Python 3.6 32-bit, so you may need to change settings in the Python.Runtime project file.
My test is in: pythonnet\src\embed_tests\pyimport.cs
Thanks
den-run-ai commented on Apr 7, 2017
@tomhunter-gh actually that pull request which I linked above #427 leads to this issue :(
Here is the code that I commented out to pass your the test:
/*if (value is IList && value.GetType().IsGenericType) { using (var resultlist = new PyList()) { foreach (object o in (IEnumerable)value) { using (var p = new PyObject(ToPython(o, o?.GetType()))) { resultlist.Append(p); } } Runtime.XIncref(resultlist.Handle); return resultlist.Handle; } }*/The reason is because before the list is passed back to C#, it gets converted to Python list.
So quick workaround would be to provide equivalent conversion from Python to C#: list -> List.
But this is of course a lot of conversion back and forth and maybe a performance issue.
den-run-ai commented on Apr 7, 2017
This is rather hackish workaround, but passes all tests! Use at your own risk :)
pythonnet_hackish.zip
tomhunter-gh commented on Apr 7, 2017
Thanks @denfromufa - that has fixed the issue!
Here's the change in case anyone else needs it:
(You also need to add a reference to Microsoft.CSharp.)
Thanks
den-run-ai commented on Jul 20, 2017
related issue:
#514
lostmsu commented on Sep 23, 2021
I believe this should already be fixed in 3.0, but we need a test.
added a regression test for pythonnet#451