7

Possible Duplicate:
c# - How do you get a variable’s name as it was physically typed in its declaration?

I'm looking for a way to get a property name as a string so I can have a "strongly-typed" magic string. What I need to do is something like MyClass.SomeProperty.GetName() that would return "SomeProperty". Is this possible in C#?

Community
  • 1
  • 1
Jim Mitchener
  • 8,835
  • 7
  • 40
  • 56

3 Answers3

9

This approach will be way faster than using Expression

public static string GetName<T>(this T item) where T : class
{
    if (item == null)
        return string.Empty;

    return typeof(T).GetProperties()[0].Name;
}

Now you can call it:

new { MyClass.SomeProperty }.GetName();

You can cache values if you need even more performance. See this duplicate question How do you get a variable's name as it was physically typed in its declaration?

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • Best answer here. I don't want to loop properties, I want to get a property name based on an actual property. Using the anonymous type with the first (only) property being the one you want the name from is genius. – Paul Jun 12 '14 at 14:15
  • @Paul I'm not the genius behind it. Secondly, it's not very refactor friendly. I mean if you change the name of the property, the anonymous type preserves the original name. In this case, if you change `SomeProperty` to `SomeProperty1`, the anonymous class changes to `new { SomeProperty = SomeProperty1 }`, which will print "SomeProperty", instead of the updated name "SomeProperty1". I will mention it in the answer. – nawfal Jun 12 '14 at 14:50
  • Darn. I don't anticipate ever changing a property name, but if I ever did, I would want the refactoring to handle it. Thanks for letting me know. – Paul Jun 12 '14 at 16:03
  • This will also fail if MyClass is null at the time of execution. Expression approaches will succeed. In the vast majority of cases that won't be an issue, because you'd be getting the null reference exception somewhere else, but it's probably worth pointing out. – Rakuen42 Mar 20 '15 at 16:16
  • @Pedro77 you have to write the extension method in a static class first. – nawfal Jun 09 '15 at 12:43
7

You can use Expressions to achieve this quite easily. See this blog for a sample.

This makes it so you can create an expression via a lambda, and pull out the name. For example, implementing INotifyPropertyChanged can be reworked to do something like:

public int MyProperty {
    get { return myProperty; }
    set
    {
        myProperty = value;
        RaisePropertyChanged( () => MyProperty );
    }
}

In order to map your equivalent, using the referenced "Reflect" class, you'd do something like:

string propertyName = Reflect.GetProperty(() => SomeProperty).Name;

Viola - property names without magic strings.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

You can get a list of properties of an object using reflection.

MyClass o;

PropertyInfo[] properties = o.GetType().GetProperties(
    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
);

Each Property has a Name attribute which would get you "SomeProperty"

David
  • 15,150
  • 15
  • 61
  • 83