Retrieve the names of all the boolean properties of a class which are true

Without LINQ:

foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
    if (propertyInfo.PropertyType == typeof(bool))
    {
        bool value = (bool)propertyInfo.GetValue(data, null);

        if(value)
        {
           //add propertyInfo to some result
        }
    }
}            

You can do it like this - all those properties that are of type bool and are true

public IEnumerable<string> Settings
{
    get
    {
        return GetType()
            .GetProperties().Where(p => p.PropertyType == typeof(bool) 
                                         && (bool)p.GetValue(this, null))
            .Select(p => p.Name);
    }
}

Tags:

C#

Reflection