Exploring reflection – Finding a value in any of the objects properties
Posted by Filip Ekberg on October 8 2011 4 Comments
Posted by Filip Ekberg on October 8 2011 4 Comments
I want to explore reflection by playing with both reflection, anonymous types and dynamically typed things all at once! My goal is not to be confusing or anything, but it is to leave out the parts that are redundant.
So let’s get to it!
I sometimes find myself having a lot of data that I want to search through, it might be the same structure for each element and it might not, I can never assume that it’s the same structure though, because if I would, there would most certainly be a case where it differs.
Let’s assume that we have two anonymous types here:
In my last post, I talked about the properties on anonymous types being read-only, the fact is that the entire anonymous object is read only, so even if I would say that it’s a dynamic anonymous type, I wouldn’t be able to specify new properties or methods on it.
Anyways! We have the above two objects that I want to search for something in, it might be the age, it might be a common string. So there are a few things that I got to figure out before I can start retrieving my results, here are the things we need to know:
- How do we get all the properties from the object?
- How do we get the value for the current property on the current object?
- How do we get the type of the object, does it matter that it’s anonymous/dynamic?
Almost all of the above have been used here and there in my previous posts, but to answer them again:
How do we get all the properties from the object?
To get all the properties, we must first get the type, so we can retrieve all the properties for that type, like this:
How do we get the value for the current property on the current object?
To get the current value of the objects property you call GetValue like this, the second parameter is null because it’s not an array:
How do we get the type of the object, does it matter that it’s anonymous/dynamic?
When you’ve retrieved the value of the property, you get the type of the value by doing this:
Now we have the basics and need to put every piece together so that we can search our objects, I want to have a method like this to find objects in a list with any property containing the Exact value that I pass:
My code that calls this method will look like this:
So what can the implementation look like? We can either solve it with LINQ or with a foreach.
My method will start out by only declaring a new list that we will put the found elements in and then I just return it to make everything build nicely:
{
var found = new List<dynamic>();
// Implementation goes here....
return found;
}
Now I need to iterate over my source-variable and for each item, I need to go through each property and see if the value matches the pattern input. I add the two nested foreach-loops like this:
{
foreach (var property in obj.GetType().GetProperties())
{
}
}
It’s now iterating over each property on each object, you don’t want to move the GetType().GetProperties() outside the foreach-loop, since in our case the structure changes over each iteration. Now we’ve come to the final part, we need to get the value of the current property on the current object and see if the type matches the pattern type and then check if the value matches the pattern:
if (pattern.GetType() != value.GetType()) continue;
if(pattern == value)
found.Add(obj);
You might not want to use the equality operator in your case, if it’s a string you might want to check if the pattern is anywhere in the current value or something like that.
This is how my Find-method turned out:
{
var found = new List<dynamic>();
foreach (var obj in source)
{
foreach (var property in obj.GetType().GetProperties())
{
var value = property.GetValue(obj, null);
if (pattern.GetType() != value.GetType()) continue;
if(pattern == value)
found.Add(obj);
}
}
return found;
}
I hope you found this interesting and if you have any thoughts please leave a comment below!

You’re not checking for pattern == null nor source == null
Also this line may throw exception if value == null
to that, you’re returning IEnumerable why not use yield?
{
var anyPropertyEqualsValue = obj.GetType().GetProperties().Any(prop=>prop.GetValue(obj, null) == value);
if(anyPropertyEqualsValue) yield return obj;
}
Hi Wojtek,
You’re right, I’ve missed to check if the source parameter is null this should either throw an exception or return null, it depends on what you want. Regarding the pattern if you might want to find properties that are null, in this case you can use this if-statement instead:
I’ve avoided using too much LINQ and new keywords like yield to make the code samples more readable and understandable by a broader audience. But it’s definitely nicer to keep the amount of lines low!
Thanks for your input
If you’re doing the if(pattern == value) later on, you don’t need to check the types. If pattern and value are the same object (after all you’re doing a reference comparison) than their (its) types must match. So this test is a bit pointless.
Secondly, pattern doesn’t change within the function, why pay the price of checking it against null in every single loop.
Thirdly, I’d make the Find-method an extension method on IEnumerable so that you can call “source.Find(pattern)”
Finally, yield creates a lazy Enumerable comparing to your eager one. Performance and memory overhead difference may be minor, but may be high too. Additionally, if source is infinite, you’re in trouble.
Imagine some quite probable code fragment:
With your implementation, the whole source has to be enumerated. In the lazy case, you’re iterating only you’ve found the first match.
Hi Wojtek,
You have a valid point, but that’s out of the scope of the blog post. I see that you like to take the functional approach in most cases.
The point of the blog post is to familiarize the readers with Reflection before we dig into more advance topics and mixing in functional programming and other more advance/different methods might confuse more than it helps.
Thanks for your input