Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

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: , , ,
Posted on Thursday, May 24, 2007 9:39 AM | Back to top


Comments on this post: C# code to rebuild properties after implementing INotifyPropertyChanged

# re: C# code to rebuild properties after implementing INotifyPropertyChanged
Requesting Gravatar...
How do I use this code to do that, do I compile it into something a module, or run from command prompt in VS2005 ??
Left by Ken on Jun 17, 2009 4:37 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free