Saving the pointer of an introspected method?

Open your bottles and flasks, I finally understood how introspection works!

But it seems to take a lot of time. Problem is this: I call an Addressbook.Find method which gives me an array of AddressbookRecords. Those by definition do only contain (despite some dates) the UniqueID property of the record, and so I used to compare the UIDs to the AddressBook.Unique IDs to obtain the other properties like FirstName.

Now, after the event mentioned above (a small step for Xojohood, but a giant one for me) I extended the record. First I’m not sure if buffering the index is fine or if it could be changed during system runtime. But I wanted to avoid searching each time again, and it gives a small speed boost so far.

But when I perform the method on more than 100 entries, this takes an awful lot of time - several seconds before the task I’m doing this in completes. For some illustration:

[code]Function FirstName(extends c as AddressBookRecord) As String
static i as integer
static found as boolean
Dim methods() As Introspection.MethodInfo = Introspection.GetType©.GetMethods
if not found then
For i = 0 To Ubound(methods)
if methods(i).Name = “FirstName” then
found = true
exit
end if
next
end if
return methods(i).Invoke©

End Function[/code]

So I wonder if it is possible to store the pointer to the method instead of introspecting the class each time. Or is it time to close the wine again and go back to learning because I’m completely wrong?

Something like this (not tested since I’m not in front of my computer):

Function FirstName(extends c as AddressBookRecord) As String Static method As Introspection.MethodInfo If method Is Nil Then Dim methods() As Introspection.MethodInfo = Introspection.GetType(c).GetMethods For Each m As Introspection.MethodInfo In methods If m.Name = "FirstName" Then method = m End Next End Return method.Invoke(c) End Function

Or this:

Function AddressBookProperty(extends c as AddressBookRecord, propertyName As String) As String Static methodDictionary As Dictionary If methodDictionary Is Nil Then methodDictionary = New Dictionary() Dim methods() As Introspection.MethodInfo = Introspection.GetType(c).GetMethods For Each m As Introspection.MethodInfo In methods If m.Name = "FirstName" Or m.Name = "LastName" Then // You can add more tests here methodDictionary.Value(propertyName) = m End Next End Return Introspection.MethodInfo(methodDictionary.Value(propertyName)).Invoke(c) End Function

Thx a lot, Eli! That makes two personal breakthroughs for me today :smiley:

The First one works like a charm already if I replace the Is by = in the initial check and add an exit to the loop. Not sure which one handled it, but before it crashed with a NILObjectException. So for reference:

Function FirstName(extends c as AddressBookRecord) As String Static method As Introspection.MethodInfo If method = Nil Then Dim methods() As Introspection.MethodInfo = Introspection.GetType(c).GetMethods For Each m As Introspection.MethodInfo In methods If m.Name = "FirstName" Then method = m Exit End Next End Return method.Invoke(c) End Function

Much, much faster!