When importing a .NET library in Python with:
import clr
clr.AddReference(r"C:\foo.dll")
from foo import *
my_foo = Foo()
print(my_foo.my_method)
# <bound method 'my_method'>
I'd like to know the signature of the function (its parameters). This doesn't work:
from inspect import signature
print(signature(my_foo.my_method))
It fails with:
ValueError: callable <bound method 'GroupStatusGet'> is not supported by signature
Question: how to get the signature of a function on Python + .NET ?
Python's built-in tools like
inspectcannot be used to examine the signature of methods from .NET assemblies. This is because Python's introspection features work primarily with Python objects and their associated metadata, and .NET assemblies are fundamentally different types of objects. Therefore,inspect.signaturewill fail with aValueErrorif you try to apply it to a .NET method.You can use .NET's own reflection capabilities to inspect the signatures of its methods. Here's how you might do this using the
System.Reflectionnamespace in .NET:Edit #1
The code example will throw
AttributeErroras mentioned in the comments. The issue here is related to the reflection API. The methodGetMethodis not an instance method, but a method from the .NET'sTypeclass.I've fixed the code (validated locally):