For various debugging processes, I find myself needing to find several bits of runtime information for my properties. This means I want to get the property’s PropertyInfo object by Reflection when that property’s code is running.
The problem is that Properties are C# constructs, not IL constructs. My property’s get accessor is actually compiled into a get_MyProp() method. This means that while I can easily get the MethodInfo for the current accessor, there doesn’t appear to be any easy way of getting the PropertyInfo from the MethodInfo.
The ugly way, of course, is to take the method’s name, strip the first four characters from it and do a GetProperty() with that name – and that works, of course, but causes me actual physical pain to use:
PropertyInfo prop = accessorMethod.ReflectedType.GetProperty(accessorMethod.Name.Remove(0, 4));
Is there a better, simpler way I’m missing?