Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if any property has been assigned a value

I have a type SearchBag that holds a bunch of strings and nullable integers to use for passing on search values. I need a way to check if the search bag contains any values.

I'm currently trying to do it like this:

    public bool HasValues()
    {
        return GetType().GetProperties().Any(p => p.GetValue(this, null) != null);
    }

But was wondering if there's a better way.

like image 914
Boris Callens Avatar asked Oct 17 '10 19:10

Boris Callens


1 Answers

Without modifying the SearchBag type, there isn't a better way.

EDIT: You could change the type to set a boolean flag in every property setter, then check the flag instead of using Reflection.

like image 131
SLaks Avatar answered Sep 28 '22 05:09

SLaks