pythonnet icon indicating copy to clipboard operation
pythonnet copied to clipboard

implement a C# Interface in Python for .NET

Open willhyper opened this issue 7 years ago • 5 comments

Environment

  • Pythonnet version: 2.3.0
  • Python version: 3.6.5
  • Operating System: Windows

Details

trying to implement a C# Interface in Python for .NET

This post exemplifies my exact problem. https://stackoverflow.com/questions/49736531/implement-a-c-sharp-interface-in-python-for-net#

However, I found relatively few discussion and solution. Forgive me raise the issue here. :) I am not sure if this is an intent for pythonnet not to support. If so, reasons are welcomed. Otherwise, I find the practice (implementing an interface) is common, and so why there is no support. My use case is, I am given a dll. The interface is defined there. I want to develop Python class that implements the interface. Without this approach viable, I foresee my developed application would then be half in C# and half in Python, which I consider ugly.

willhyper avatar May 18 '18 18:05 willhyper

@willhyper is this something what you are looking for? scroll to the end of the issue with comments how this was implemented in pywebview:

https://github.com/pythonnet/pythonnet/issues/567

den-run-ai avatar May 21 '18 07:05 den-run-ai

@denfromufa Awesome! namespace is the missing single line that solves my question!

willhyper avatar May 24 '18 05:05 willhyper

great, then let's close this issue. feel free to comment back.

den-run-ai avatar May 24 '18 15:05 den-run-ai

I was recently confused by this and had to do alot of googling to find the answer on Stack Overflow.

It would be a great improvement to have

  • an example in the docs
  • an example of implementing the interface in test_interface.py

mkimberling avatar Apr 21 '20 19:04 mkimberling

The documentation is seriously incomplete in this area. All it currently says about Python subclasses of .NET base classes is here:

Managed classes can also be subclassed in Python, though members of the Python subclass are not visible to .NET code. See the helloform.py file in the /demo directory of the distribution for a simple Windows Forms example that demonstrates subclassing a managed class.

Since methods are "members", this made me think that overriding methods was not possible. And the linked example did nothing to correct that misunderstanding.

Perhaps it would be better to link to these tests, which inherit from these C# base classes.

mhsmith avatar Jul 11 '23 13:07 mhsmith