Self Aware Objects – The Beginning

I’ve had an idea for a while that programming objects should be self-aware.

Within your class definitions, you should be able to define default values and validation rules for your properties.  Of course, you can do that right now by using the WALOC method (Write A Lot Of Code) in your setters, but I’d rather find a more elegant solution.

So I started playing around with creating custom attributes for properties.

Unfortunately, most of the samples I’ve found on line about how to create custom attributes, and connect them up to your properties, doesn’t exactly work.

Here’s what I have right now, running in C# with the .Net 4.0 Framework.




Workbench.cs (the code for a Windows form testing app)

What this gets me

This is what I have as my starting point.  I’ve actually created some other validation attributes, but I just want to have the basics in this post.

What I’m eventually thinking of doing making it so that if you ever set a property to an invalid value, it will raise an event.  Then, anything watching that event (like your user interface) can immediately do some sort of alert.

I’d also like to have it so your error messages can be easily created from within your properties, in whatever language you want (using resource files).

Leave a Reply

Your email address will not be published. Required fields are marked *