"Kirk Jackson" <[EMAIL PROTECTED]> wrote: > I was looking at the "Attribute Based Validation" examples > on the newtelligence website > (http://www.newtelligence.com/news/AttributeBasedValidation.aspx), > which made me wonder about how attributes work... > > I know that you can declare attributes on a class/member etc, > and query the attributes in your code using reflection (which is > what the above sample does), but how do you get the attribute > to _automatically_ cause code to be run on a field / method / > class access?
In general you can't. For example, on page you linked to, the clue is in the line: public bool Validate() { return Constraints.Validate(this).Length > 0; } This method is calling into some method (Constraints.Validate) which will actively search for the attributes it cares about. There are certain special exceptions: > The PrincipalPermission attribute is an example of the behaviour > I am thinking of - when there is a PrincipalPermission attribute > on a method, the framework(?) automatically calls Demand() > to check the users authentication. Is it possible to have other > attributes automatically called? There are a whole bunch of attributes which are 'magic', in the sense that the runtime is the thing that looks for them, so stuff happens automatically when they are present. This gives the impression that the attributes are causing code to be run automatically. But all that's really happening is that the framework looks for them. Having said that, there is the whole context architecture. I should warn you that the context architecture used to be documented (in Beta 2 of the framework) but they took out all the docs for the first release. This is presumably because they plan to change it all. So whilst it is interesting, although it works, it probably won't work with the next release... But this did let you define so-called 'context attributes', which if applied to an object which derives from ContextBoundObject would hook into the .NET interception layer. But right now there is no documented way of getting attributes of your devising automatically instantiated. -- Ian Griffiths DevelopMentor You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.