lundi 24 avril 2017

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

I have a class that has lots of bool properties. How can I create another property that is a list of strings that contains the name of the properties which have a value of true?

See initial attempt below - can't quite figure out how to filter the true ones

public class UserSettings
{
    public int ContactId { get; set; }
    public bool ShowNewLayout { get; set; }
    public bool HomeEnabled { get; set; }
    public bool AccountEnabled { get; set; }

    // lots more bool properties here

    public IEnumerable<string> Settings
    {
        get
        {
            return GetType()
                .GetProperties()
                .Where(o => (bool)o.GetValue(this, null) == true) //this line is obviously wrong
                .Select(o => nameof(o));
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire