views:

72

answers:

1

Well, I am not alone who is being sick of magical strings which are constantly used just to do data binding -- see:

http://stackoverflow.com/questions/1329138/how-to-make-databinding-type-safe-and-support-refactoring

However I am concerned with performance of this solution and much more typing -- each time you would like to use nameof you have to type a lambda. So I am thinking of more brute-force method -- writing a function nameof and external program, that will change each .cs file before it is compiled.

public string nameof<T>(T obj,string name = null)
{
  return name;
}

You would use it this way

nameof(the_object.MyProperty);

The key lies in external program helping nameof -- it would search for any call of the nameof, and simply replace

nameof(X.Y.Z)

or

nameof(X.Y.Z,"s")

into

nameof(X.Y.Z,"Z")

My question is -- what limitations, pitfalls of this approach do you see? Or maybe .Net 5.0 with built-in nameof will be shipped next week, so it make no sense to start writing a program for it? ;-)

Thank you in advance for your thoughts, recommendations, and so on.

+3  A: 

Doing any pre-compilation code changes would be an extreme hassle. So I would avoid it.

I wouldn't worry too much about performance - most times when you need a property name as a string you'll end up doing reflection or IO anyway, so performance is going to be relatively slow anyway.

Here's what I suggest to use:

public static string ToPropertyName<T>(this Expression<Func<T>> @this)
{
    var @return = string.Empty;
    if (@this != null)
    {
        var memberExpression = @this.Body as MemberExpression;
        if (memberExpression != null)
        {
            @return = memberExpression.Member.Name;
        }
    }
    return @return;
}

Now you can do this:

Expression<Func<T>> px = x => x.Foo;
var pn = px.ToPropertyName(); // == "Foo"
Enigmativity
What is this @return notation? I've never seen this before.
Alex
@Alex: this is to confuse other programmers. It allows you to use keywords as variable names.
Henrik
Thank you for pointing out performance issue. Now I am thinking about putting for every property I need name of it, a static readonly property which holds its name, this way, the name would evaluated once.
macias
@macias, that's not a bad idea at all. That would be similar to dependency properties in WPF...
Thomas Levesque
@Enigmativity - the extension method is neat but you would have to declare the expression beforehand... passing the expression directly as a parameter would make for cleaner source code: GetPropertyName(x => x.Foo);
Peter Lillevold
@`Alex` - I use the `@return` variable to make it obvious what variable has my return value. Also the `@this` variable clearly lets me know what object the extension method applies to. I would consider this notation confusion if I used "out of context" keywords - for example, using `@using` or `@for` in this code would not convey any meaning and would be confusing.
Enigmativity
@`Peter Lillevold` - I do create methods like `NotifyPropertyChanged(Expression<Func<T>> propertyExpression)` for exactly the purpose you describe. My calling code is `this.NotifyPropertyChanged(() => this.Name)` and the `NotifyPropertyChanged` method then simply calls the extension method.
Enigmativity