Absolutely - use Type.GetCustomAttributes
. Sample code:
using System;
using System.ComponentModel;
[Description("This is a wahala class")]
public class Wahala
{
}
public class Test
{
static void Main()
{
Console.WriteLine(GetDescription(typeof(Wahala)));
}
static string GetDescription(Type type)
{
var descriptions = (DescriptionAttribute[])
type.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptions.Length == 0)
{
return null;
}
return descriptions[0].Description;
}
}
The same kind of code can retrieve descriptions for other members, such as fields, properties etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…