Intensive Attribute Programming [Coding]
Intensive Attribute Programming - Searching of Attributes
One common problem with attribute is to perform a search of it. DotNet do not have function to make deep search of attributes and forces developers to make a search for necessary attribute manually.
For example, how to find classes with has methods with some attributes?
You need to make a loop on all loaded assemblies, all classes in them, all methods in them, all attributes of those methods, and if u find matching, add it to your result list.
I’m sure this is not looks very simple. And of course, this executes to fast – if you enterprise level application use attributes, it requires searches like that. And it is important to find this result quickly.
What my code do?
My code, provided below, solves 2 problems:
- One function to search for all attribute include derived from specified class – it can search for attributes assigned to class itself, methods, properties, other members.
- All results are cached Hashtable. Next time when application performs this search, result will be ready immediately.
How to use it?
Examples of usage attached in Unit test project.
Result returned as Hashtable. Key is element to which matching attribute applied to; Value – Array List of all attributes matching your criteria.
Future extensions
Maybe it will be good to provide SQL-style search query for function, etc. I’m sure that you can derive implementations specific for your code.
DB Configurable attributes
If your team start to use custom attribute searching function, u will be able to configure attributes dynamically, for example, from DB.For example, MandatoryAttribute set on business object field, could be turned on or off without needs to recompile (and redeploy!) you system.
Sources: OsypchukAttributeSearcher.zip
Applies to all dotnet languages: C#, VB.NET, C++.NET, J#

0 Comments:
Post a Comment
<< Home