jeudi 5 novembre 2020

Get class and instance DescriptionAttribute values

How do you retrieve the DescriptionAttribute of an instance of a class and of the class itself?

[Description("The Help class description")]
public class Help
{
    //...
}

public class General
{
    [Description("The Help instance description")]
    public Help ThisHelp {get; set;} = new Help();
}

public static class Program
{
    static void Main()
    {
        General MyGeneral = new General();
        var Atts = MyGeneral.ThisHelp.GetType().GetCustomAttributes(typeof(DescriptionAttribute), true);
        Console.WriteLine(string.Join(Environment.NewLine, Atts.OfType<DescriptionAttribute>().Select(a => a.Description)));
        //Only writes "The Help class description" ... But not the instance
    }
}

I get the class version only.





Aucun commentaire:

Enregistrer un commentaire