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;
}
}
}