Perhaps Expression Trees can help you here.
Instead of Foo_Method , taking the property name as a string
void Foo_Method(string name) { }
use an Expression type parameter to get the property name using MemberExpression :
void Foo_Method(Expression<Func<NameOfYourClass, String>> exp) { var propertyName = ((MemberExpression)exp.Body).Member.Name; }
and name it as follows
public string Name { get { return lcl_name; } set { if (lcl_name != value) { lcl_name = value;
Thus, when renaming Name you do not break your code, since Foo_Method(x => x.Name) also renamed (using the IDE's refactoring capabilities to rename, of course).
Edit:
To respond to your comment:
If you really cannot add overloading to Foo_Method, you can, of course, just create another method:
if (lcl_name != value) { lcl_name = value; Foo_Method(GetPropName(x => x.Name)); } ... string GetPropName(Expression<Func<NameOfYourClass, String>> exp) { return ((MemberExpression)exp.Body).Member.Name; }
Edit2:
To respond to another comment:
You can create an extension method
public static class Extensions { public static string GetPropName<T>(this T t, Expression<Func<T, String>> exp) { return ((MemberExpression)exp.Body).Member.Name; } } var propertyName = yourInstace.GetPropName(y => y.Name);
but you don’t need this as expressions work fine without any instance.
public static class Extensions { public static string GetPropName<T>(Expression<Func<T, String>> exp) { return ((MemberExpression)exp.Body).Member.Name; } } var propertyName = Extensions.GetPropName<YourClass>(y => y.Name);
the trick is to use generics here.
The first method will look like this in VB.Net
Public Function GetPropName(Of TClass, TProperty)(exp As Expression(Of Func(Of TClass, TProperty))) as String Return DirectCast(exp.Body, MemberExpression).Member.Name End Function ... GetPropName(Function(x) x.Name)