3

I have been working on using mvp to wire up some winforms in C#. UI development is not exactly my strong suite and I was looking for a refactor proof way to implement INotifyPropertyChanged without having to resort to strings to fire change notifications.

The msdn example http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx and most of the others I have seen strike me as the wrong way to do it least of all with regards to the fact that the strings have to be changed in case you refactor the properties.

All the other options I have seen on stackoverflow using expressions and func, facilities sadly not available in the legacy solution I am supporting so I was wondering if any one has a way to replicate the current solutions that use expression trees to get the property name.

An example of the solutions I have been seeing.

public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } 

Cheers

7
  • 1
    ... refactor proof ... really ...really??? Commented Jun 10, 2012 at 17:52
  • @Bernard Which examples do you have that you assume are not supported in .NET 3.0? The new features I can think of in 3.5, 4.0 and the upcoming version are parallel extensions, optional and named parameters, dynamics and async, however, none of those seem relevant to the issue. Commented Jun 10, 2012 at 18:06
  • @DannyVarod, this is an example of the function I have been seeing around public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } Commented Jun 10, 2012 at 18:21
  • I am pretty sure that will compile in 3.0. Commented Jun 10, 2012 at 19:12
  • @DannyVarod, I have tried it, expression trees were introduced in 3.5 unfortunately msdn.microsoft.com/en-us/library/… Commented Jun 14, 2012 at 8:15

1 Answer 1

6

For .NET 3.0, your best bet to implement a refactor-proof way of implementing INotifyPropertyChanged is through some form of code-generation. For example, PostSharp (an AOP tool) uses IL-weaving to modify the generated assembly in a post-compilation step. You can find a relevant sample for INotifyPropertyChanged here.

By the way, the state of the art (C# 5) way of implementing INotifyPropertyChanged requires no special tools at all: there's the [CallerMemberName] attribute.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ani, thanks for the example, looks good , I'll try it out and report back.. Cheers !
you've been very helpful, I tried the PostSharp way in the link you provided but it uses aop inheritance which isn't part of the free distribution. Cheers again
Ended up doing it the traditional way , as we arent yet on C#5

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.