0

I used a decompiler to extract all the code from a DLL (SharpSVN -- cannot get my hands on the source code) and I want to modify an enum to give it a DisplayName.

public enum SvnStatus
{
  Zero,
  None,
  [DisplayName("Not Versioned")]
  NotVersioned,
  //other one-word values that don't need a display name
}

But this gives me the following error:

Attribute 'System.ComponentModel.DisplayNameAttribute' is not valid on this declaration type. It is valid on 'Class, Method, Property, Event' declarations only.

Googled around and found a number of threads where people seem to do this no problem with their enums. Am I missing something? I can't Resolve the error in Visual Studio, the option doesn't even show up (but that might be because I just installed Resharper and am not familiar with it yet?)

Edit: Just found DevExpress has a CustomColumnDisplayText event where I can change the value as desired, so I'm going to go with that instead, since the data is only being displayed in a GridControl.

sab669
  • 3,984
  • 8
  • 38
  • 75

1 Answers1

2

The reason is given in the error you're getting.

The System.ComponentModel.DisplayNameAttribute has been attributed with a System.AttributeUsageAttribute which constricts the usage to only be applied to classes, methods, properties, or events. Enums are excluded. It looks like this:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event)]

Perhaps you can write your own attribute instead?

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • Well what I don't get is why something like [this question](http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) uses the DisplayName attribute on his enums. I saw it used in a number of other questions on SO as well. Would I be able to just use the `[Description]` attribute instead? Unfortunately something went wrong with the decompling and I can't even build the solution it gave me so I guess I'm SOL either way. – sab669 Nov 07 '13 at 18:31
  • He's not using the `DisplayName` attribute, he's clearly using an attribute called `Display`. – Trevor Elliott Nov 07 '13 at 18:37
  • Oh, right, sorry, I had seen `[Display(Name = "")]` and `[DisplayName("")]` in different places and neither worked for me. – sab669 Nov 07 '13 at 18:38