![]() |
Languages »
C# »
How To
Intermediate
Filtering object collections using reflectionBy Richard J SladeAn article that shows how to filter a strongly typed collection using reflection. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
We all know that when writing software, re-use is of importance to get the most out of the time we have. When we apply this to object collections, we should be aiming to write collection agnostic code that can be used over and over again. For example, when we use a collection of say User objects, we should be able to give it the same operation as we give a collection of Car objects. The operation that we pass to the collection shouldn't care if the collection has cars, users or elephants. It just knows how to perform its operation.
I stumbled on a great article recently on sorting object collections using reflection. The PropertySorter would be passed to the sort method of any strongly typed collection and would sort it depending on the values passed. Doing the same thing with filtering was therefore my aim: to be able to have a strongly typed collection that would implement an IFilterable interface and take in a ReflectiveFilter.
The features of the ReflectiveFilter are as follows:
FilterOperand to decide if you want to place And or Or between the FilterProperties.
FilterClause to decide if you want to place a Equals or IsNotEqualTo in the filter criteria.
AddFilter method of the ReflectiveFilter. To use the ReflectiveFilter is easy. Simply instantiate a new instance of the filter, add the filters you need, and the Filter method will pass back a strongly typed collection that is filtered on your requirements.
Example: We have a collection of names and IDs and want to filter it so we only get back names that are equal to Phil or Richard.
// In the reflective filter, we pass it (at the least)
// the typeof object that is contained in the collection we are filtering
// We also pass in the type of collection we arefiltering (and expect back)
ReflectiveFilter rFilter = new
ReflectiveFilter(typeof(CompanyName.DomainObjects.ConstrainedType.NameIdReference),
typeof(CompanyName.DomainObjects.ConstrainedType.NameIdReferenceCollection),
FilterClause.Equals, FilterOperand.Or);
// add filters
// as above we have used the OR operator and the Equals clause
// this means we will get all records where
// the name=Richard OR name=Phil
rFilter.AddFilter("Name", "Richard");
rFilter.AddFilter("Name", "Phil");
ACollectionType filteredCollection = new NameIdReferenceCollection();
filteredCollection = nameIdReferenceCollection.Filter(rFilter);
As you can see from the example, we pass in up to four parameters.
type of object contained in the collection you are filtering (required).
type of collection being filtered (required).
FilterClause (optional / defaults to FilterClause.Equals).
FilterOperand (optional / defaults to FilterOperand.Or). The FilterClause can be set to either Equals or IsNotEqualTo and defaults to Equals.
The FilterOperand can be set to either Or or And, and defaults to Or. The job of the FilterOperand is when we have more than one filter applied, to either specify that we need to look for (for example) records where Parameter=Value Or/And Parameter=Value.
The default constructor accepts just two parameters:
typeOfObject: The type of object that is contained in the collection we are filtering.
typeOfCollection: The type of collection that is being filtered. The ReflectiveFilter seems to perform quickly and because it is collection agnostic, means that most other requirements for custom filters are not needed.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 25 Oct 2005 Editor: Smitha Vijayan |
Copyright 2005 by Richard J Slade Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |