1

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:

enter image description here

Debugger in C#.net:

enter image description here

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.

3
  • 2
    Try CTypeDynamic. Commented Oct 16, 2021 at 18:03
  • Ahh....nice, look like it could be the solution :-) But I see it was introduced in .core 3.0 and I still haven't upgrade from 2.2 yet so I can't confirm yet. I did though find a method in the Python.net library which do the conversion. Commented Oct 16, 2021 at 18:26
  • Glad you found a way to convert to the needed type. FYI: dynamic creates a DLR object. As you discovered, this is not the same as a non-strict VB object. Hence the newer CTypeDynamic as per Jeroen's comment. But DynamicObject class has been around since .core 1.1, so I suspect there was some way to do it in VB, even without CTypeDynamic. Commented Jan 7, 2022 at 2:32

1 Answer 1

1

Beside the solution from @Jeroen Mostert, which I prefer as it's more universal , I did find a method in the Python.net library that do the job: pyObject.As<T>()

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 PyObject = Cache.keys()
   Dim PKGs As String() = PyObj.As(Of String())() '<==========
   Console.WriteLine(PKGs(0))
End Using
PythonEngine.BeginAllowThreads() 'Must be called to release the Py.GIL thread!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.