I have a struct
with fields I want to make into a human-readable list. I created a DisplayName
attribute that holds the string I want to appear in the final list:
public struct ColumnMeta {
[DisplayName("Coords.")]
public Vec2i Location;
[DisplayName("Age")]
public TimeSpan ChunkAge;
public byte ChunkSize;
}
I can easily print all of the names via
var fields = typeof(ColumnMeta).GetFields();
foreach (var f in fields) {
foreach (Attribute att in f.GetCustomAttributes(typeof(DisplayNameAttribute))) {
DisplayNameAttribute dnAtt = (DisplayNameAttribute) att;
Console.WriteLine(dnAtt.name);
}
}
This prints Coords.
and Age
as it should.
However, if I have an instance of ColumnMeta
, how can I iterate over fields that have the DisplayName
attribute?