Do INotifyPropertyChanged users use reflection to fish out the value from the actual property?
When I implement INotifyPropertyChanged
does the code using the event use reflection to fish out the value from the actual property? Or use 开发者_运维问答it use something other than reflection like IL emit?
No, because the PropertyChangedEventArgs
raised by the event only contains the sender of the event and the name of the property which changed on the sender. If you would like access to the old or new value, you must retrieve them on your own.
As for what the Framework uses to determine what the value is currently, that will depend on the context used (one option is a PropertyDescriptor
from a TypeDescriptor
).
If you're talking about how the WPF data binding infrastructure gets to the data, it does some fairly sophisticated things. I can only speak to what I've seen from the disassembled source as I last saw it, as these are implementation details and subject to change. For non-Dependency properties (the ones that are typically notified using INotifyPropertyChanged), the system uses Reflection internally, but caches the references. The Binding Path property string is represented as a PropertyPath and there is an internal PropertyPathWorker that does the work of parsing this path and setting up the actual binding. This creates a cached 'list' of references, since the property path can be rather complex, such as Something.SomeProperty[0].SubProperty.(Grid.Row)
. The binding system allows for any of the values along the chain to change and, if any of them changes, has to reevaluate the downstream references.
In short, it's designed to be efficient, but doesn't use Emit, as it has to support re-computation of the references based on changes inside a complex path. This can be further complicated by the dynamic nature of the binding, since the actual Type of a reference along the chain can change. For instance, Something
may be of type Object, so any object that has a SomeProperty
IList<T>
property is a valid binding target. If the Type changes, it has to re-reflect to bind to the new property on the new Type.
The client code does whatever it wants to.
Typically, InotifyPropertyChanged
is consumed by the databinding infrastructure, so it will get property values using PropertyDescriptor
s.
INotifyPropertyChanged
does nothing but tells the class to raise event. When or which property will raise event depends on your code and how you implement it. You can use reflection or Attribute (ContextBoundObject) or can use attribute to inject IL code inside setter method of property
精彩评论