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
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)
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. 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.
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.