Reputation: 9704
I have written an abstract class that I am using to automate a lot of INotifyPropertyChanged and IDataErrorInfo stuff. However this requires that I specify a custom getter/setter which calls a custom method for every property.
To avoid the extra typing I have been trying to find a way to override/handle the property getters/setters in an object and call the custom method instead of the generated getter/setter.
I tried inheriting from DynamicObject and overriding TryGetMember and TrySetMember, however these methods only seem to work if the object is declared as dynamic.
So I want to know if what I am trying to achieve is possible at all through .NET reflection or some other mechanism, also is there anyway to detect if the property setter/getter has been defined in code?
Thanks, Alex.
Upvotes: 2
Views: 987
Reputation: 6911
You can get information about properties of a type using Type.GetProperties method. You will receive a collection of PropertyInfo object. Those object have CanRead
and CanWrite
properties which say if properties are readable/writable.
To override this behavior you would have to:
For free solutions you are best to use Castle DynamicProxy. If you are ready to spend some money, take a look at PostSharp which already implements many things of similar nature. Like INotifyProperty chage, undo/redo etc. You could also take a look at any AOP framework which supports aspect weaving, but DynamicProxy would be my pick for situation you described.
Upvotes: 3