vendredi 7 octobre 2016

C# using propertyinfo ( reflection ) in LINQ statement

Instead of setting each boolean property in object manually. I would like to do it for all boolean properties using reflection.

//For each boolean property: if any in collection is true, result is true.
private ActionFlagsViewModel StackActionFlagsViewModels(List<ActionFlagsViewModel> afList)
{
    var result = new ActionFlagsViewModel();
    foreach (var propInfo in typeof(ActionFlagsViewModel).GetProperties().Where(p => p.PropertyType == typeof(System.Boolean)))
    {
        // TODO: Do this using reflection for each property info.
        result.ShowActionAbort = afList.Where(afvm => afvm.ShowActionAbort == true).Any();
        result.ShowActionAssign = afList.Where(afvm => afvm.ShowActionAssign == true).Any();
    }
    return result;
}

This should be very easy right ?





Aucun commentaire:

Enregistrer un commentaire