14

Say, for example, I've got this simple class:

public class MyClass
{
  public String MyProperty { get; set; }
}

The way to get the PropertyInfo for MyProperty would be:

typeof(MyClass).GetProperty("MyProperty");

This sucks!

Why? Easy: it will break as soon as I change the Name of the Property, it needs a lot of dedicated tests to find every location where a property is used like this, refactoring and usage trees are unable to find these kinds of access.

Ain't there any way to properly access a property? Something, that is validated on compile time?
I'd love a command like this:

propertyof(MyClass.MyProperty);
Sam
  • 28,421
  • 49
  • 167
  • 247
  • 3
    It gets worse. If you look at WPF, you'll notice that it requires string property names for data binding and also when implementing INotifyPropertyChanged. This is now not a corner case for users of Reflection, but the general everyday use-case. I currently rely on Resharper to help keep things in sync. – Dan Bryant Jun 08 '10 at 13:31
  • Unitesting helped me alot here, but thats about it... – Enriquev Jun 08 '10 at 13:45
  • Related stack overflow question with more detailed answers: http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property – Steve Cadwallader Jan 19 '13 at 14:15
  • possible duplicate of [how-to-get-the-propertyinfo-of-a-specific-property](http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property) – nawfal Dec 13 '13 at 12:10
  • 1
    You can use 'typeof(MyClass).GetProperty(nameof(MyProperty);' in c#6 – cedd Oct 09 '15 at 10:57

3 Answers3

6

The closest you can come at the moment is to use an expression tree:

GetProperty<MyClass>(x => x.MyProperty)

and then suck the PropertyInfo out in GetProperty (which you'd have to write). However, that's somewhat brittle - there's no compile-time guarantee that the expression tree is only a property access.

Another alternative is to keep the property names that you're using somewhere that can be unit tested easily, and rely on that.

Basically what you want is the mythical infoof operator which has been talked about many times by the C# team - but which hasn't made the cut thus far :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Looks intriguing - but how would I write GetProperty? I'm not even sure *what* it is - a generic class? Or just a generic method? infoof sounds lovely, too bad it's not in (yet). – Sam Jun 08 '10 at 13:09
3

In the time since this question was posted, C# 6 has been released with the nameof operator. This allows a property to be accessed with the following

PropertyInfo myPropertyInfo = typeof(MyClass).GetProperty(nameof(MyClass.MyProperty));

If you rename the property, this code will not compile (actually it will, since the rename will change this line of code as well if the rename is done properly).

-2

The whole point of reflection is to be able to access stuff at runtime. If we assume your operator would work, you already have the class information and thus the property, making the whole thing completely useless.

Femaref
  • 60,705
  • 7
  • 138
  • 176