[Description("This is a wahala class")]
public class Wahala
{
}
В любом случае, чтобы получить содержимое атрибута Description для класса Wahala?
Ответ 1
Абсолютно - используйте Type.GetCustomAttributes. Пример кода:
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;
}
}
Такой же код может извлекать описания для других членов, таких как поля, свойства и т.д.
Ответ 2
Использовать отражение и атрибут .GetCustomAttributes