Skip to content

Convert python objects to CLR objects dynamically #484

@yagweb

Description

@yagweb

Environment

  • Pythonnet version:
  • Python version: all
  • Operating System:

Details

  • Convert python objects to CLR objects is a basic need when embedding python. It can be done with the 'AsManagedObject' method if the target CLR type is known in the compiling time, but now I need to it dynamically.

    For example, suppose a python function returns a list, in which a element can be of type bool, int, str, list, dict etc. I want to convert the list to a List<object> in CLR. I write a class for this and it works well, which used a Dictionary<IntPtr, Func<PyObject, object>>.

    However, the code looks ugly, because the python types in Runtime class, such as PyIntType, are internal, so I use 'PythonEngine.Eval("int").Handle' to get the type instead. (XDecref should be called for the type as key)

    My question is, is there a better way to implement the dynamically converting? if not,
    could pythonnet expose this types for users by some way, such as a method with signature 'IntPtr GetPythonType(string type))'?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions