Dynamically Calling Methods

ss7thirty

Well-known member
Joined
Jun 14, 2005
Messages
455
Location
New Jersey, US
Programming Experience
5-10
I have a configuration based application that dynamically creates controls based on XML configuration file. This is a scanning application and the only bump that I am hitting for this project is the fact that for each different configuration I need to execute different code in 4 different events.

I need to execute different code for each configuration in the Batch Start, Batch End, Page Start, and Page End events of the scan object.

I was thinking of using the addhandler command and point it to the proper function based on the configuration, but I cannot figure out how to do this. Can anyone point me in the right direction as to which objects I have to use to dynamically call the methods for these events based on the configuration without hard coding a giant select statement.
 
Not exactly sure what you mean with "dynamically call the methods for these events", you don't call methods for events, you raise events from your class or they are being raised from their class.
The addhandler is very basic to its functionality (AddHandler object.event, AddressOf handler), not sure what you can't figure out.
 
I thought that my post was a bit unclear after reading over it. What I want to be able to do is dynamically specify the sub-routines that will handle the events. I know I can do this through addhandler but when I do this, it is not dynamic. I have to specify an exact method for that event to go to. Instead I want to be able to pass some sort of variable which would indicate which sub-routine will now handle the event.


For example you can run dynamic SQL statements that you pass variables to decide which table it will run on. Naturally, this is completely different but the same in that I want to be able to pass some sort of variable so that I can tell the application which sub-routine will run based on the configuration when certain events are raised.

Does this make things clearer? It is very possible that I am not even going about this in the right way. But you have no idea how much time I will save if I can get a single application for hundreds of clients rather than hundreds of applications for hundreds of clients.
 
Last edited:
An example doing it with reflection, a button is added to form and Click event assigned to "handler" method:
VB.NET:
    Sub dynamicEvent()
        Dim t As New Button
        Dim evi As Reflection.EventInfo = t.GetType().GetEvent("Click") 
        Dim d As System.Delegate = System.Delegate.CreateDelegate(evi.EventHandlerType, Me, "handler")
        evi.AddEventHandler(t, d) 'or AddHandler t.Click, d
        Me.Controls.Add(t)
    End Sub

    Sub handler(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("dynamic event handler")
    End Sub
It's described more thoroughly in How to: Hook Up a Delegate Using Reflection
You don't need EventInfo (evi) if you know the event delegate type, or define it explicitly (Public Event SomeEvt As theDelegateType), the key is the CreateDelegate. For example Button.Click delegate is System.EventHandler.

I can't tell if you really need to do thing this way, one would probably have to review your whole structure/code to see if things can be done differently and easier.
 
You are the best...That was a major issue. Based on what I told you I need I could then have a string in each of the XML configuration files representing the method that I would like to run for the 4 events based on a users logon / windows authentication.

I will probably run the code from another class and/or code file. Therefore, I would also like to be able to configure which class is being passed at the following line:

VB.NET:
 Dim d As System.Delegate = System.Delegate.CreateDelegate(evi.EventHandlerType, Me, "handler")


So I have the following question. Is it possible to store objects in a configuration file. Because each sub-routine is going to have to contain different code and I do not want to have so many methods in a single code file. Is it possible to do this with a partial class and still pass me? After a while it may be possible that I could have 2000 methods in a single class if I use the partial class. Won't this slow down if it all has to compile when I am only going to use 4 of them at a time.

I know there is a plethora of ways to do this. Possibly with modules in the same namespace within the same code file may be an option too.
 
Is it possible to store objects in a configuration file.
Yes it is, there is also My.Resources.
Is it possible to do this with a partial class and still pass me?
Partial classes have to be compiled with the assembly, they can't be added afterwards. "Me" keyword represent current instance of the class it is called from.
After a while it may be possible that I could have 2000 methods in a single class if I use the partial class. Won't this slow down if it all has to compile when I am only going to use 4 of them at a time.
With many partial classes it could take longer to compile, I don't think it will affect runtime performance.
It is technically possible to have an uncompiled class and many partial classes and compile for example only the class and one partial at runtime (with commandline compiler vbc.exe), then load it and create instance with reflection. In design time code you would handle these classes through an Interface. This is similar to the common plugin approach, see for example Writing Plugin-Based Applications
Possibly with modules in the same namespace within the same code file may be an option too.
Namespaces provide organization, classes from different assemblies can belong to the same namespace, but a single class can't be divided over more than one assembly.
 
Back
Top