Differentiate methods using instrospection

Hello,

I would like to know how to differentiate 2 exact methods (name+parameters) using Instrospection

Using Instrospection.TypeInfo.GetMethods(), I have the 2 methods that shows up but I do not know how to differentiate them.

Class Parent
  Sub createDict(level As Integer)
  End Sub
End Class

Class Child Inherits Parent
  Sub createDict(level As Integer)
   super.createDict(level)
   //Additional stuff there
  End Sub
End Class

Regards,

Julien

Introspection works on instances. In your case, if you inspect an instance of child, the createdict method that would be returned in GetMethods would be the one on the child class. If you wanted to access the one in the parent class, you would need to first cast the instance to the parent class.

Var parentItem as Parent = Parent(childInstance)
Var mi() as MethodInfo = Instrospection.TypeInfo.GetMethods(parentItem)

Thank you for your answer,

But the issue I am seeing in my code is that the “creatDict” method shows up twice in the array of methods returned by GetMethods.

I am using the GetMethods only on the Child instance, I never do an instance of the parent.

The array is sorted by text asc with the methods of the parent coming first a-z then the methods of the current instance (the child) a-z

Maybe something else is wrong with my code too

Edit: I am using 2025r1 on macOS Sonoma

If you have control over the classes, you could use an attribute. Something like “class” = “parent”.

Then you could query that at runtime. I haven’t made it to my computer yet to check any of this.

Edit:
Ah, from the docs:

TypeInfo.GetMethods lists parent methods which are overridden, resulting in duplicates in the list when you call GetMethods on a subclass.

It may be that invoking either of the methods will call the one on child though… since they’re just strings. You’d have to check that.

Well I did read the doc but missed that, thank you. I should be more careful.

I can always reverse the loop to start from the end to find my method but if Xojo apply a change in the order returned by GetMethod, my code won’t work any longer.

As what you suggested, adding something in the parent class. The thing is I want to call the “createDict” of the parent pretty much 99% of the time. Only when the child has the same method, I would like to call it.

That’s the behavior you’ll get if the child is a subclass of the parent and overrides the parent class’s method, unless you do as Greg suggested and explicitly cast the child as an instance of the parent class.

Using the Introspection system is often a yellow flag, as it can be used as a workaround to circumvent Xojo’s inherent object oriented design. :grin: What are you trying to accomplish here? We might be able to suggest a more efficient path - the Introspection system has a bit of reputation for adding a lot of overhead to your app.

2 Likes

I just got a note from Norman Palardy (another ex-engineer) about this. He said:

The list of methods is actually predictable (thanks [to] Joe Ranieri (yet another ex-engineer)).

child methods will be later in the list, so if you iterate backwards, the child instance would be the first one you encountered.

this holds true for all types accessible via introspection.

1 Like

I use the introspection to serialize/de-serialize data coming from our API.

We have a loop through each properties and parsing it. In case of a class or an array of class, we parse it and create instances of it.

In this method its to serialize the class into a dictionary to send it to the remote API.

Like Eric said: That’s what the object-oriented paradigm already takes care of – if there is a method defined for a subclass it takes precedence (overrides) a method of the superclass. No introspection needed.

Whenever one thinks one needs introspection, chances are there is a better approach without resorting to introspection.

1 Like