Extract method name as string from reflection string or hard coding?

I am calling a method from a property. I need to pass the property name as an attribute of this method. My property

string lcl_name = string.Empty; public string Name { get { return lcl_name; } set { if (lcl_name != value) { lcl_name = value; Foo_Method(GetCorrectPropertyName(MethodBase.GetCurrentMethod().Name)); } } } 

and method

 public string GetCorrectPropertyName(string propertyName) { return propertyName.StartsWith("set_") || propertyName.StartsWith("get_") ? propertyName.Substring(4) : string.Empty; } 

My seniors say I shouldn't call Reflection and pass a straight line to the method this way

 Foo_Method("Name"); 

but in this case, since it will be hard-coded and if the property name is changed, then the method call must be changed accordingly.

So my question is which one will be better in terms of efficiency? Is there anything else that my elderly people see that I don’t pay attention to?

+2
c #
Sep 10
source share
1 answer

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; // Foo_Method("Foo"); string is bad Foo_Method(x => x.Name); } } } 

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) 
+2
Sep 10 '12 at 9:21
source share



All Articles