Added `ToPythonAs<T>()` by lostmsu · Pull Request #2330 · pythonnet/pythonnet

Added ToPythonAs<T>() extension method to allow for explicit conversion using a specific type

What does this implement/fix? Explain your changes.

The existing ToPython() extension always uses current object type. There are scenarios though where objects have to be explicitly represented as their base types or interfaces. For example, 42.ToPython() produces Python int, but there's currently no easy way from C# to pass 42 as IConvertible so that Python could call the IConvertible members.

The new extension can be used for this purpose: 42.ToPythonAs<IConvertible>() will return a PyObject that has all IConvertible members.

Does this close any currently open issues?

fixes #2311

Any other comments?

N/A

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Updated the CHANGELOG