views:

298

answers:

3

When i store a delegate (that points to a page method) in session state, retrive it after a postback and execute it the target of the delegate is the old page object and not the current one, is there anyway to change the target of the delegate so that it executes the method on the current page object ?

I have thought about using a static page method but then i don't have access to the controls on the page which defeats the object of what i am trying to do, which is update a text box.

+3  A: 
delegateInstance.Method.Invoke(obj, arguments);
Mehrdad Afshari
you are frickin awesome !
Note that this is meant as a last resort. I would use a delegate to a **static** method instead and pass the object as an argument.
Mehrdad Afshari
+1  A: 

Make your delegate take the new page as its first parameter, and when you call it pass this.

Jon Skeet
This seems like a better approach than @Mehrdad's, since it's easier to get the intention when you read the code.
codekaizen
+6  A: 

I'm a little nervous about this. Both Jon Skeet's and Mehrdad's suggestions will work (of course), but page instances are supposed to be disposed after a request completes. This delegate could be preventing that from happening, leading to the .Net equivalent of a memory leak.

Perhaps you could use reflection and put a System.Reflection.MethodBase or System.Reflection.MethodInfo object in the session to invoke later instead.

Joel Coehoorn
This makes me nervous, for the reasons you stated. I'd rather persist just the part of the state we care about, not the entire page.
Steven Sudit