8000 Added `ToPythonAs<T>()` by lostmsu · Pull Request #2330 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Added ToPythonAs<T>() #2330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2024
Merged

Added ToPythonAs<T>() #2330

merged 1 commit into from
Feb 28, 2024

Conversation

lostmsu
Copy link
Member
@lostmsu lostmsu commented Feb 27, 2024

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

@lostmsu lostmsu mentioned this pull request Feb 27, 2024
5 tasks
@filmor filmor merged commit 9d18a24 into pythonnet:master Feb 28, 2024
@lostmsu lostmsu deleted the ToPythonAs branch February 28, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects 4972
None yet
Development

Successfully merging this pull request may close these issues.

Allow setting typed objects via PyModule.Set
2 participants
0