3

I currently use reflection to grab all the property names in a class of a specific type or access level. I then run these through RaisePropertyChanged() to essentially "update" the entire view.

An instance where I do this is on startup, when the program starts and when the ViewModel is instantiated it will run this to ensure the view is showing all the correct data from the model.

Is there anything wrong with doing this?

Code if you guys want it:

    private void InitializeViewModel()
    {
        foreach (string name in MiscMethods.GetPropertyNames(this))
        {
            RaisePropertyChanged(name);
        }
    }

    public static IEnumerable<string> GetPropertyNames(Object yourClass)
    {
        foreach (PropertyInfo property in GetProperties(yourClass))
        {
            yield return property.Name;
        }
    }

    //Uses Reflection to return all properties in a class
    private static IEnumerable<PropertyInfo> GetProperties(Object theObject)
    {
        return theObject.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance);
    }
Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • I'm not sure there's anything inherently wrong with doing this, but I'm curious as to what prompted you to implement it in the first place. Were you having issues getting your UI to show the right values? – goobering Jul 30 '15 at 09:03
  • possible duplicate of [C#/WPF: PropertyChanged for all Properties in ViewModel?](http://stackoverflow.com/questions/1859946/c-wpf-propertychanged-for-all-properties-in-viewmodel) – LzyPanda Jul 30 '15 at 09:10
  • To me this suggests that you're binding to models that don't support property change notification to begin with and you're trying to hack around it. A better solution, IMO, is to use something like Castle Dynamic Proxy to [inject that functionality into your models](http://www.codewrecks.com/blog/index.php/2008/08/04/implement-inotifypropertychanged-with-castledynamicproxy). – Mark Feldman Jul 30 '15 at 13:22

2 Answers2

8

Rise event with empty string

RaisePropertyChanged("");

this will trigger update for all properties.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • That's hilariously simple. Here I was trying to come up with an ingenious way to do it. I wonder though, does calling the method like that utilize reflection to find all the properties anyways? – Douglas Gaskell Jul 30 '15 at 09:03
  • Nope, `INotifyPropertyChanged` define an event to which view subscribed when you using binding. Once this event handler is created reflection is not needed anymore. Empty string as parameter for this event is threat in special way, causing all bindings to update (no idea how it's done tbh). – Sinatr Jul 30 '15 at 09:09
  • That's what I meant, after you fire that I imagine it must still sue reflection to find all the property names. Not saying I wont use it, however I'm curious if it ends up doing the same thing in the background. – Douglas Gaskell Jul 30 '15 at 09:17
  • I read [this](https://msdn.microsoft.com/en-us/library/bb613546(v=vs.100).aspx) once some long time ago. – Sinatr Jul 30 '15 at 09:30
0

Horrible idea. A) reflection is slow, you aren't even caching the stuff B) why would you update 50 properties when you only updated 1?

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86