sab669
sab669

Reputation: 4104

Cannot set a DisplayName on an enum

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.

Upvotes: 0

Views: 2294

Answers (1)

Trevor Elliott
Trevor Elliott

Reputation: 11252

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?

Upvotes: 2

Related Questions