---
title: "HOWTO: Let the user Filter object property values dynamically"
url: "https://osherove.com/blog/2003/2/22/howto-let-the-user-filter-object-property-values-dynamically.md"
canonical: "https://osherove.com/blog/2003/2/22/howto-let-the-user-filter-object-property-values-dynamically.html"
published: "2003-02-22T20:07:00"
updated: "2003-02-22T20:07:00"
tags: []
author: "Roy Osherove"
description: "I am currently creating a project where a user can see trace messages from older applications written in VB 6.0. we have a trace utility that throws traces using COM . iIdecided i would try to write a client in C#. the…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# HOWTO: Let the user Filter object property values dynamically

By Roy Osherove February 22, 2003

**Historical article** · Originally published February 22, 2003; technical details may be outdated.

I am currently creating a project where a user can see trace messages from older applications written in VB 6.0. we have a trace utility that throws traces using **COM**.  iIdecided i would try to write a client in C#. the user should have the ability to filter the traces that he sees, based on regular expressions.

So i have my TraceMessage Objects with their properties and stuff, and i created a FILTER object that decides whether a certain message passes the filter. however, i **DID NOT** want the filter to be built Hard coded to the properties of the Message, as the Message could take many shapes and can (in the future) come from many sources and have different properties.

**Reflection to the rescue** - I created a small function that uses reflection and lets the user decide for each filter, what property he wants to check, and the needed value it should have. it turned out pretty easy, and pretty cool. My FINAL goal i think would be to let the user use not only regular expression since those need learning and not everyone knows them ,but to let the user write "AND (X OR Y) OR Z..." etc... still have no idea how to do that though.

there must be a way to evaluate expressions in .NET, shouldnt there... if anyone knows how, please notify me. Thanks. Anyway, heres the code:

 

//the actual work:

Public bool Passes(TraceMessage msg)
  {
   try
   {
    Type t  = msg.GetType();
    PropertyInfo pinfo = t.GetProperty(m\_NeededName);
    if (pinfo==null)
     return false;
    if (pinfo.CanRead )
    {
     string val =  (string)pinfo.GetValue(msg,null).ToString();
     if (Regex.IsMatch(val,m\_NeededValue))
     {
      return true;
     }
    }
    return false;
   }
   catch(Exception e)
   {
    throw e;
   }
   
  }

//How to use this:

private void OnTrace(TraceListener listener,ArrayList traces)
  {
   Filter f = new Filter("Message","=");
   foreach(TraceMessage msg in traces)
   {
    if (f.Passes(msg))
     WriteTrace(msg);
   }
  }


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
