Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Custom Objects Searchable through Reflection

Rate me:
Please Sign up or sign in to vote.
3.14/5 (4 votes)
28 May 2009CPOL2 min read 22.8K   83   9   9
Make your custom objects searchable simply by annotating them.

Image 1

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:

C#
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)


Written By
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 Pin
Donsw14-Jun-09 5:35
Donsw14-Jun-09 5:35 
Generalthanks Pin
Yankee Imperialist Dog!28-May-09 9:25
Yankee Imperialist Dog!28-May-09 9:25 
GeneralRe: thanks Pin
I-Flite30-May-09 23:49
I-Flite30-May-09 23:49 
GeneralMy vote of 1 Pin
zlezj28-May-09 9:07
zlezj28-May-09 9:07 
GeneralRe: My vote of 1 Pin
r_hyde28-May-09 13:34
r_hyde28-May-09 13:34 
GeneralRe: My vote of 1 Pin
I-Flite31-May-09 0:06
I-Flite31-May-09 0:06 
GeneralRe: My vote of 1 Pin
I-Flite30-May-09 23:59
I-Flite30-May-09 23:59 
GeneralNot a bad implementation Pin
Peter Lange28-May-09 8:19
Peter Lange28-May-09 8:19 
GeneralRe: Not a bad implementation Pin
I-Flite30-May-09 23:55
I-Flite30-May-09 23:55 
Glad you like it, Peter. I exstracted the code from a project I'm currently working on, which is a tailor made customer handling system for a sports club. The system integrates with an RFID-reader, enabling the club members to register their visits by swiping their RFID cards.

The system is expected to undergo extensive changes in the months following deployment, so I wanted a generic, dynamic way to search for customers, even when the Customer class changes rapidly.

Espen

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.