I have defined a class with a single function. For example:
namespace my.namespace
{
public class MyClass
{
public void some_func(string s1, string s2)
{
// more code here
}
}
}
I am able to load this object into an ironpython interpreter. I want to use introspection to get a list of methods that were implemented only in this class. In this example I want a list like ['some_func']. Is there a way to do it?
If I do a help(instance) on this instance I get more-or-less what I want:
class MyClass(object)
| MyClass()
|
| Methods defined here:
|
| __repr__(...)
| __repr__(self: object) -> str
|
| some_func(...)
| some_func(self: MyClass, s1: str, s2: str)
Of course, when I d a dir(instance) I get a lot of other functions:
>>> dir(instance)
['Equals', 'GetHashCode', 'GetType', 'MemberwiseClone', 'ReferenceEquals', 'ToString', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'some_func']
I want to know what instrospection method I need to use to get a list of only the functions unique to this class.
You have a number of options.
You can (explicitly) implement the
IronPython.Runtime.IPythonMembersListinterface that way you can list whatever members you want to list. It’s as if you defined the__dir__method for your class.You could always define a public
__dir__method for your class as well. The return type could be anything really, but you’ll probably want to return some collection of strings.You always have the option to use regular .NET reflection.