Click here to Skip to main content
12,949,083 members (62,245 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 28 May 2009

Custom Objects Searchable through Reflection

, 28 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Make your custom objects searchable simply by annotating them.


Every once in a while, when dealing with GUI applications, you want to allow the user to search or filter collections. When presenting users with a large lists of items, e.g., customer names, it might be too time consuming for the user to manually sift through the entire list, even if it's sorted by name. And, what if you want to enable the user to search for different customer properties, like name, street, and city? Sure, you could make several search-fields, or present the user with radio-buttons which indicate what property they are searching for. But, for a fast and user-friendly application, a meta-search-field may be preferred. While presented with only one input-field, the user is actually searching through multiple properties on each item simultaneously. Let's say the user searches for the term "Los Angeles", - behind the scenes, the application automatically searches through many properties on each customer, e.g., name, street, and city. All customers living in Los Angeles are presented.

While this functionality could be implemented in many ways, the SearchableDemo shows how this can be done in a flash using Reflection and annotation. This enables you to make your existing C# classes searchable in no time! Simply inherit the Searchable class and annotate the private or public fields you want to be searchable.

How does it work

By extending (inheriting) the abstract class "Searchable", your objects inherit the public method "bool match(String searchPhrase)". This method uses Reflection to inspect your inheriting class, and looks for fields you have annotated with the "[Searchable]" custom attribute. All annotated fields are then searched for the phrase, and if found, "True" is returned.

Using the code

Below is an example of how to use the abstract class and the annotation:


As you see from the example above, a simple Customer class inherits the abstract class "Searchable". Also, the private fields "name", "street" and "city" are annotated with "[Searchable]". Together, this is all you need to make your class searchable. So now, all your GUI-application needs to perform a search is some simple presentation-code:

foreach (Customer customer in customers)
//the collection of customers to be searched
    if (customer.match(textBox4.Text))
    //textbox4 contains the user search input
        //display all matching customers

So, when your demanding end users want to be able to search yet another field (which they, of course, previously didn't think they needed), simply add "[Searchable]" to the new field!

Points of interest

The "[Searchable]" attribute isn't limited to strings only, you may apply it to any other type of object as well. The "match()" method calls the object's "ToString()" method, enabling the search to cover all kinds of objects. Be aware though, not all objects have meaningful "ToString()" return values.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Norwegian Defence Research Establishment
Norway Norway
Proffessional bio at

You may also be interested in...

Comments and Discussions

Generalgood intro Pin
Donsw14-Jun-09 5:35
memberDonsw14-Jun-09 5:35 
Generalthanks Pin
Yankee Imperialist Dog!28-May-09 9:25
memberYankee Imperialist Dog!28-May-09 9:25 
GeneralRe: thanks Pin
Member 463820330-May-09 23:49
memberMember 463820330-May-09 23:49 
GeneralMy vote of 1 Pin
zlezj28-May-09 9:07
memberzlezj28-May-09 9:07 
GeneralRe: My vote of 1 Pin
r_hyde28-May-09 13:34
memberr_hyde28-May-09 13:34 
GeneralRe: My vote of 1 Pin
Member 463820331-May-09 0:06
memberMember 463820331-May-09 0:06 
GeneralRe: My vote of 1 Pin
Member 463820330-May-09 23:59
memberMember 463820330-May-09 23:59 
GeneralNot a bad implementation Pin
Peter Lange28-May-09 8:19
memberPeter Lange28-May-09 8:19 
GeneralRe: Not a bad implementation Pin
Member 463820330-May-09 23:55
memberMember 463820330-May-09 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 28 May 2009
Article Copyright 2009 by I-Flite
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid