开发者

Attribue to verify field is initilized by a given point in an ASP.Net page lifecycle

开发者 https://www.devze.com 2023-01-06 17:57 出处:网络
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.N

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" exc开发者_开发问答eption 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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消