Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Description attribute on Enum giving error

Tags:

c#

.net

I am getting the following error from the description attribute as follows:

Attribute 'Description' is not valid on this declaration type. It is only valid on 'method' declarations.

public enum TestEnum
{
    [Description("Property 1")]
    Property1,
}

I have never had this error before and Google isn't shedding much light. I guess it is a reference issue, but I'm not really sure.

like image 477
rhughes Avatar asked Oct 28 '25 09:10

rhughes


1 Answers

The DescriptionAttribute can be applied to virtually any type of member, including enum values. Are you sure you're using the right [Description]? Make sure you reference the assembly it comes from:

using System.ComponentModel;

And, if you do happen to have two different attributes called 'Description' in your scope, prefix it with the namespace to remove the ambiguity, i.e.

enum MyEnum {
    [System.ComponentModel.Description("Blah")]
    MyValue
}
like image 59
Bradley Smith Avatar answered Oct 31 '25 11:10

Bradley Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!