I have a pseudo-enum class that consists of a protected constructor and a list of readonly static properties:
public class Column
{
protected Column(string name)
{
columnName = name;
}
public readonly string columnName;
public static readonly Column UNDEFINED = new Column("");
public static readonly Column Test = new Column("Test");
/// and so on
}
I want to access individual instances by their string name, but for some reason, the reflection does not return the static properties at all:
In the above image, you can see that the property exists and has a non-null value, yet if I query it using reflection, I get null
.
If I try to query the property list, I get an empty array:
PropertyInfo[] props = typeof(Column).GetProperties(BindingFlags.Static);
if (props.Length == 0)
{
// This exception triggers
throw new Exception("Where the hell are all the properties???");
}
What am I doing wrong?
You are trying to access fields, not properties.
Change your reflection code to this:
FieldInfo[] fields = typeof(Column).GetFields();
if (fields.Length == 0)
{
// This exception no longer triggers
throw new Exception("Where the hell are all the properties???");
} else
{
foreach (var field in fields)
{
Console.WriteLine(field.Name);
}
}