Ever wonder how that magical tag called ‘[Serializable]’ placed at the top of a class enables it to be deconstructed into XML that can be tranmitted through firewalls or persisted in a files or database tables? Not to get too technical but attribute classes are embedded into the tagged classes’ metadata at compilation and can be reflected on to perform any function you desire.
The following example provides a real world scenario where custom attributes can enhance one’s coding experience. I can’t tell you how many times I have to use stored procedures that have a million parameters each having the capacity to be null. Think of a search procedure that has ten criteria. If I was coding this with the SqlCommand class, I would have to write a method that creates each SqlParameter and populates it with DBNull or some value. There are over ninety nine combinations in this scenario. That really cuts into my ebay time.
If I wrote an attribute that could turn a class into a SqlCommand with populated values and could be serialized in session or view state and reduce my time coding, life would be wonderful. In addition, if I wrote a tool that generated the code for the attributed class, my evil plan to do no work would be complete!
I have attached a solution that contains all the code mentioned above.
AtrributeSolution.zip (51.43 kb)
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5