Automatic INotifyPropertyChanged Implementation through T4 code generation?

Posted by chrischu on Stack Overflow See other posts from Stack Overflow or by chrischu
Published on 2010-06-03T17:39:00Z Indexed on 2010/06/03 17:54 UTC
Read the original article Hit count: 617

Filed under:
|
|

I'm currently working on setting up a new project of mine and was wondering how I could achieve that my ViewModel classes do have INotifyPropertyChanged support while not having to handcode all the properties myself.

I looked into AOP frameworks but I think they would just blow up my project with another dependency.

So I thought about generating the property implementations with T4.

The setup would be this: I have a ViewModel class that declares just its Properties background variables and then I use T4 to generate the Property Implementations from it.

For example this would be my ViewModel:

public partial class ViewModel
{
    private string p_SomeProperty;
}

Then T4 would go over the source file and look for member declarations named "p_" and generate a file like this:

public partial class ViewModel
{
    public string SomeProperty
    {
        get
        {
            return p_SomeProperty;
        }
        set
        {
            p_SomeProperty= value;
            NotifyPropertyChanged("SomeProperty");
        }
    }
}

This approach has some advantages but I'm not sure if it can really work. So I wanted to post my idea here on StackOverflow as a question to get some feedback on it and maybe some advice how it can be done better/easier/safer.

© Stack Overflow or respective owner

Related posts about c#

Related posts about T4