The value of attributes
I often rediscover the value of Attributes in C#. Very often it is hard to find a situation where you have need of attributes, you may be thinking that it is only for meta coding like unit testing or dynamic code intepretation/serializing, but you're wrong. Attributes are useful every time you need to add meta data to a class, property or field. Let me give you the common example of adding label to an enum.
Problem: An enum defines genre in your music database. Now you want to display all available genres.
Start by defining the attribute Label.
public string Text { get; set; }
}
Now you can add this to your genre values.
[Label("Alternative Rock")]
AlternativeRock = 1,
[Label("Hard rock")]
HardRock = 2,
[Label("World music")]
WorldMusic = 4,
[Label("Indie")]
Indie = 8,
[Label("Post-rock")]
PostRock = 16
}
With the simple use of an extension method you may accomplish alot.
FieldInfo field = typeof(T).GetField(val.ToString());
LabelAttribute[] attributes =
(LabelAttribute[])field.GetCustomAttributes(typeof(LabelAttribute), false);
if (attributes.Length == 0)
throw new ArgumentOutOfRangeException("Enum value " + val.ToString() + " is missing Label attribute");
return attributes.First().Text;
}
}
Now you may easily type out all available genres with a simple procedure.
foreach (Genre genre in Enum.GetValues(typeof(Genre)))
Console.WriteLine(genre.Label());
That's all for today. Have a nice weekend!