Tim Hibbard

CEO for EnGraph software
posts - 629 , comments - 1681 , trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

C# code to rebuild properties after implementing INotifyPropertyChanged

This code is meant for people that want to easily implement INotifyPropertyChanged after using the "prop" code snippet.  It will take code that looks like this:

public int DriverID
{
   get { return _driverID; }
   set { _driverID = value; }
} 

And return it like this:

public int DriverID
{
   get { return _driverID; }
   set
   {
      _driverID = value;
      OnPropertyChanged("DriverID");
   }
}

It also will ignore any triple slash comments and #regions.  So you can copy your entire #region --Properties-- into the class and it will return a #region --Properties-- will the new OnPropertyChanged code.

Here is the code:

using System;
using System.Collections.Generic;
using System.Text;

namespace EnGraph.Helpers
{
    public class InsertINotify
    {
        public static string RebuildProperties(string value)
        {
            string rv = "";
            try
            {
                StringBuilder sb = new StringBuilder();
                string[] tmp = value.Split(char.Parse("\n"));
                string propertyName = "";
                foreach (string text in tmp)
                {
                    string val = text.Replace("\r", "");
                    if (val.Replace("  ", "").StartsWith("public"))
                    {
                        sb.Append(val + Environment.NewLine);
                        propertyName = val.Replace("  ", "").Split(char.Parse(" "))[2];
                    }
                    else if (val.Replace("  ", "").StartsWith("set {"))
                    {
                        sb.Append("set" + Environment.NewLine);
                        sb.Append("{" + Environment.NewLine);
                        string setText = val.Replace("set { ", "");
                        setText = setText.Replace(" }", "");
                        sb.Append(setText + Environment.NewLine);
                        sb.Append("OnPropertyChanged(\"" + propertyName + "\");" + Environment.NewLine);
                        sb.Append("}" + Environment.NewLine);
                    }
                    else
                    {
                        sb.Append(val + Environment.NewLine);
                    }
                }
                rv = sb.ToString();
            }
            catch (Exception)
            {
                rv = "";
            }
            return rv;
        }

    }
}

 

Technorati tags: , , ,

Print | posted on Thursday, May 24, 2007 9:39 AM |

Feedback

Gravatar

# re: C# code to rebuild properties after implementing INotifyPropertyChanged

How do I use this code to do that, do I compile it into something a module, or run from command prompt in VS2005 ??
6/17/2009 4:37 AM | Ken
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: