views:

9

answers:

1

I would like to create an attribute I can decorate specific fields or properties with. This attribute will throw an exception if the field or property it decorates is null at a given point in an ASP.Net page lifecycle. I.E. if a property, "x", is null at the prerender stage of an ASP.Net page, it will cause a "ArgumentNullException" exception to be throw with a nice message. Or a mean message. Either way, a message of my choosing.

Is this possible with standard .Net attributes, or would some kind of Spring.Net magic be required? Any guidance appreciated.

Thanks

A: 

Now, I understand your setup. It's just your actual question that confuses me.

You would need to define your own attribute class, derived from the CLR Attribute class. It would just need a parameter indicating at which points to check.

Then you would need a class derived from Page, which will be the base class for you pages, which, at the apprpriate time (e.g. PreRender() etc), it will use reflection to scan through the members of the page, looking for ones which have the attribute.

UPDATE (responding to comments): The attributes are just data tacked onto the PropertyInfo. It's never "executed" by the framework. You can look for it if you want, and if you find one of your attributes, you can call a method on it, but that has to be done by something outside of the attribute. (I know nothing about Spring, so I can't help you there)

James Curran
Hi James - thanks for your response. I have some user controls that use Delegates to push the responsibility for "fetching" data into the containing page. For this to work, the delegates must be set by the page by, say, page load time. I currently have the page load method of the user control checking that the required delegates are not null. This works fine. However, it would be nice to have an attribute to decorate the delegates with which could do all this for me.I can see how the approach you suggested would work. Is there a way to achieve this all from within the attribute?
James
Cont... We have access to the page via HttpContext.Current.CurrentHandler. From with the custom attribute class, maybe it is possible to set an anonymous method to the page load event on the page - which would check for the value? From within an attribute, is it possible to check the value of the member the attribute is decorating? Or maybe this is possible through Spring.Aop (http://www.springframework.net/doc-latest/reference/html/aop.html). These are not things I have used much of, so I am just looking for some guidance. Maybe I am barking up the wrong tree!Thanks for any help.
James