The first idea that comes to mind is to use a dictionary to store all the delegates under one or more keys each. This would require initializing the dictionary and all the delegates prior to being used but it can be done:
Dim methods As New Dictionary
Dim caller As MethodCaller = AddresssOf MethodHelloWorld
methods.Value("MethodWorld") = caller
I believe that there is no need of the Dim “caller” part.
Dim methods As New Dictionary
methods.Value("MethodWorld") = AddressOf MethodHelloWorld
methods.Value("MethodDolly") = AddressOf MethodHelloDolly
MethodCaller(methods.Value("MethodWorld")).Invoke
Tony: As far as I know, the introspection system allows me to get the name of my methods, but I can’t get the address of a method just by knowing its name. Please do correct me if I’m wrong.
Andrew L, Rick A.: Using a dictionary works, but I’d rather not do it because of the same reasons I don’t want to use a select-case sentence: I’d have to add a new branch (or a new dictionary entry) every time I add a new method. The dictionary option is better than a select-case though, so if a don’t come with anything better it would be my way yo go.
Massimo: I still haven’t looked into the Decorator and Factory examples, but I’ll give you my feedback as soon as I can get to it. Thank you very much.
You can use Introspection to invoke methods of an object by getting a MethodInfo array for the object.
e.g.
Dim methods() As Introspection.MethodInfo() = Introspection.GetType(TargetObject).GetMethods
For i As Integer = 0 To Ubound(methods)
methods(i).Invoke(Param1, Param2, Param3[, ...])
[quote=24222:@Andrew L.]You can use Introspection to invoke methods of an object by getting a MethodInfo array for the object.
e.g.
Dim methods() As Introspection.MethodInfo() = Introspection.GetType(TargetObject).GetMethods
For i As Integer = 0 To Ubound(methods)
methods(i).Invoke(Param1, Param2, Param3[, ...])
[/quote]
As Darth Vader once said: “Impressive. Most impressive”
I can’t try this code right now, but it’s the first thing i’m going to do as soon as I get home.