I have some doubt about the title, but I couldn’t come up with anything better.
Say I have the following enum
public enum ClassProperties
{
Primary = 0,
Secondary = 1,
}
And a class that looks this
public class Test
{
Primary { get { return _primary; }}
Secondary { get { return _secondary; }}
// more irrelevant properties
}
Now somewhere along the line I need to iterate over the enumeration and use each item in it to get the property, like so:
foreach(ClassProperties myProp = Enum.GetValues(typeof(ClassProperties)))
{
Test t = new Test();
t.myProp // <- this is what I'm after
// so if myProp equals Primary,
// t.Primary is called...
}
This would give you an idea of what I’m trying to do, but trying it makes me feel dirty like a bum who just wet himself. It just doesn’t feel right.
Well you could use Reflection to retrieve the properties. This will then locate the property based on its name.
For more see GetProperties() method & the returned PropertyInfo type.