Ata
Ata

Reputation: 12544

make description for a property

How can I make a description for a property and receive it via system.reflection ?

Upvotes: 1

Views: 85

Answers (2)

Aliostad
Aliostad

Reputation: 81660

There is already an attribute for it:

System.ComponentModel.DescriptionAttribute

although you can make yours if you want.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could use a custom attribute:

public class FooAttribute : Attribute
{
    public string Description { get; set; }
}

public class Bar
{
    [Foo(Description = "Some description")]
    public string BarProperty { get; set; }
}

public class Program
{
    static void Main(string[] args)
    {
        var foos = (FooAttribute[])typeof(Bar)
            .GetProperty("BarProperty")
            .GetCustomAttributes(typeof(FooAttribute), true);
        Console.WriteLine(foos[0].Description);
    }
}

Upvotes: 3

Related Questions