Dynamic Method/Event Invocation

R

RodBillett

I guess I am just brain dead, but I cant figure this out for the world.

in our asp.net application, I have a base page that ensures some logic is
performed (or has been performed in the past) for some page. This logic
consists of some setup and validation processes.

I am wanting our customers to be able to extend this functionality if they
would like but i dont want them to have source access to our code.

In reality - what I would like is for my base page to contain some pre and
post type events. Like PreDoSomething( SpecialEventArgs ) and
PostDoSomething( SpecialEventArgs ). Then if I detect that the customer has
placed the special dll in the bin directory, i could use reflection to
dynamically create an instance of an object and hook up to the desired
events.

The customers dll then has their custom extension logic in it. they could
even cancel the event execution if they so desire by returning a flag from
the event (throwing an error, etc)

Could anyone point me in the proper direction on this? I am not sure if
reflection is the correct way to go. I have looked at some of the
httpmodule example, but they appear to be specifically related to
application/session events and not to stuff related to a developers
application logic.

thanks in advance
Rod
 
C

Chris Jackson

You can just create an assembly that contains a class that derives from
System.Web.UI.Page. Distribute this assembly, and then your customers need
only to reference this assembly and inherit from this page in order to get
your functionality. As for providing events, there is little reason for you
to implement reflection. Your customers can subscribe to these events and
put handlers in as appropriate - the whole point of delegates is that they
will call all of its subscribers for you so that you don't need to.
 
R

RodBillett

Chris Thanks for your reply. I should have gone into more detail on my
initial post - But I believe I have solved my problem.

Our customers are not recompiling our entire application, we are wanting
them to extend our functionality. The situation exists in that our base
page calls a DLL that performs some authorization logic, but if desired - we
want the customers to be able to implement their own custom authorization
logic and *Plug in* the DLL. here is how I have solved the problem.

I create an interface IAuthorizationLogic that contains a few methods.
For our default processing I create a class that implements the Interface
methods. (It also contains some non-overidable helper functions.
If a customer wishes to extend or override the default authorization logic,
they inherit from our default processing class and override the necessary
functions.

Within primary our code - I check the configuration to see if the
CustomAuthorization flag (and its dll name) exists. if it does not, I just
instantiate the default object methods. If the Custom DLL exists, I load
it, create an instance of the correct type and then Invoke the method
through reflection.

In my sample application it worked fine.

The problem with the delegate approach (the approach I was trying when I
initially posed this thread) was that I had to have created an instance of
the custom class and have it subscribe to the events in order for the custom
processing to occur which isnt what we wanted. We only want to instantiate
the custom class IF it is going to be used. AND the customer cannot modify
our original source code.

Thank
Rod
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top