Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Custom Objects Searchable through Reflection

0.00/5 (No votes)
28 May 2009 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here