I cannot cast an object to a string-array (or collection) in VB.net, I am able to do it in C#.
VB.net code:
Runtime.PythonDLL = "/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.so"
Using Py.GIL
Dim APT As Object = Py.Import("apt")
Dim Cache As Object = APT.Cache()
Dim PyObj As Object = Cache.keys()
Dim PKGs As String() = CType(PyObj, String())
End Using
I get the exception Unable to cast object of type 'Python.Runtime.PyObject' to type 'System.String[]'.
C#.net code which work:
Runtime.PythonDLL = "/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.so";
using (Py.GIL())
{
dynamic APT = Py.Import("apt"); // Type: dynamic {Python.Runtime.PyModule}, <module 'apt' from '/usr/lib/python3/dist-packages/apt/__init__.py'>
dynamic Cache = APT.Cache(); // Type: dynamic {Python.Runtime.PyModule}, <apt.cache.Cache object at 0x7fa701ec6760>
dynamic PyObj = Cache.keys(); // Type: dynamic {Python.Runtime.PyModule}, ['0ad', '0ad-data',...]
String[] PKGs = (String[])PyObj;
}
I see the same in the debugger for vb.net and C#.net.
Debugger in vb.net:
Debugger in C#.net:
I'm aware I use the dynamic type in C# and Object in VB. I do use option strict=Off in VB.
As fare I know VB doesn't have dynamic but I assume you can use Object instead if strict=Off.
Maybe that's the reason and it's just not possible?
I also tried to convert to:
- arraylist
- List(of String)
- HasetSet(of String)
- Dictionary(Of String, Object)
Tried both TryCast, DirectCast and CType.


CTypeDynamic.dynamiccreates aDLRobject. As you discovered, this is not the same as a non-strict VB object. Hence the newerCTypeDynamicas per Jeroen's comment. ButDynamicObjectclass has been around since .core 1.1, so I suspect there was some way to do it in VB, even withoutCTypeDynamic.