Click here to Skip to main content
Click here to Skip to main content

Custom Objects Searchable through Reflection

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

Introduction

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:

searchableCode1.gif

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
        listBox1.Items.Add(customer);
        //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.

License

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

About the Author

I-Flite
Software Developer Norwegian Defence Research Establishment
Norway Norway
Proffessional bio at http://www.linkedin.com/pub/espen-skjervold/4/985/1b8

Comments and Discussions

 
Generalgood intro PinmemberDonsw14-Jun-09 5:35 
Generalthanks PinmemberYankee Imperialist Dog!28-May-09 9:25 
GeneralRe: thanks PinmemberMember 463820330-May-09 23:49 
GeneralMy vote of 1 Pinmemberzlezj28-May-09 9:07 
GeneralRe: My vote of 1 Pinmemberr_hyde28-May-09 13:34 
GeneralRe: My vote of 1 PinmemberMember 463820331-May-09 0:06 
GeneralRe: My vote of 1 PinmemberMember 463820330-May-09 23:59 
GeneralNot a bad implementation PinmemberPeter Lange28-May-09 8:19 
GeneralRe: Not a bad implementation PinmemberMember 463820330-May-09 23:55 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140705.1 | Last Updated 28 May 2009
Article Copyright 2009 by I-Flite
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid