vendredi 1 février 2019

How to get all same properties from multiple classes?

SITUATION

I want to display all general properties of multiple classes. Lets say there is a class "A" with properties "a" and "c" , and there is a class "B" with properties "b" and "c" ... so i basically want to get the property "c"

Is there any easy way to do this?

CODE

 private void GetAllProperties(ObservableCollection<DUIElement> selecteditems)
    {
        if (selecteditems == null)
            return;

        _properties.Clear();

        foreach (DUIElement item in selecteditems)
            foreach (PropertyInfo property in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (property.PropertyType.Name != nameof(DPropertyViewModel))
                    continue;

                foreach (DUIElement itemnext in selecteditems)
                {
                    if (item.GroupName == itemnext.GroupName)
                        continue;

                    foreach (PropertyInfo propertynext in itemnext.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (propertynext.PropertyType.Name != nameof(DPropertyViewModel))
                            continue;

                        if (property.PropertyType.Name == propertynext.PropertyType.Name)
                        {
                            var prop = propertynext.GetValue(itemnext, null) as DPropertyViewModel;
                            _properties.Add(prop);
                        }
                    }
                }
            }

    }





Aucun commentaire:

Enregistrer un commentaire