Casey Crookston
Casey Crookston

Reputation: 13965

Create a custom DisplayAttribute for enums?

I've got an enum class...

public enum LeadStatus : byte
{
    [Display(Name = "Created")] Created = 1,
    [Display(Name = "Assigned")] Assigned = 2,
    ....
}

Name of course is out-of-the-box. From MetaData...

namespace System.ComponentModel.DataAnnotations
{
    public sealed class DisplayAttribute : Attribute
    {
        ...
        public string Name { get; set; }
        ...
    }
}

Suppose I wanted my own custom Display Attribution, such as "BackgroundColor"...

[Display(Name = "Created", BackgroundColor="green")] Created = 1

I've seen a few other threads here that kinda dance around the issue, but the context is different enough that I can't make it work. I assume I need to create some sort of extension / override class, but I am not picturing this in my head.

Thanks!

Upvotes: 2

Views: 1835

Answers (3)

Casey Crookston
Casey Crookston

Reputation: 13965

having concluded this this isn't possible, I went with another kind of solution. Not as tidy as I had hoped for originally, but it still gets the job done.

Methods inside enum in C#

Upvotes: 0

CodingYoshi
CodingYoshi

Reputation: 27039

public sealed class DisplayAttribute : Attribute is a sealed class and therefore you cannot inherit it and add other behavior or properties to it.

Below is my assumption but someone can chime in if they know why

And you may wonder why .NET developers made it sealed? I am wondering the same and my assumption is because each of the properties in DisplayAttribute are used to inject javascript, html etc. If they left it open, and you added a BackgroundColor property to it, what does that mean? What would that do in the UI?

Upvotes: 2

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19179

Having your own attribute.

public sealed class ExtrasDisplayAttribute : Attribute
{
    public string Name { get; set; }
    public string BackgroundColor { get; set; }
}

And this extension method.

namespace ExtensionsNamespace
{
    public static class Extensions
    {
        public static TAttribute GetAttribute<TAttribute>(Enum value) where TAttribute : Attribute
        {
            return value.GetType()
                .GetMember(value.ToString())[0]
                .GetCustomAttribute<TAttribute>();
        }
    }
}

Now you can extract attribute from enum like this.

using static ExtensionsNamespace.Extensions;

//...

var info = GetAttribute<ExtrasDisplayAttribute>(LeadStatus.Created);
var name = info.Name;
var bg = info.BackgroundColor;

//...

public enum LeadStatus : byte
{
    [ExtrasDisplay(Name = "Created", BackgroundColor = "Red")] Created = 1,
    [ExtrasDisplay(Name = "Assigned")] Assigned = 2,
}

If you want to still use the original attribute you can have that too. you should apply both attributes to single enum.

public enum LeadStatus : byte
{
    [Display(Name = "Created"), ExtrasDisplay(BackgroundColor = "Red")]Created = 1,
    [Display(Name = "Assigned")] Assigned = 2,
}

And extract each one you want.

var name = GetAttribute<DisplayAttribute>(LeadStatus.Created).Name;
var bg = GetAttribute<ExtrasDisplayAttribute>(LeadStatus.Created).BackgroundColor;

Upvotes: 3

Related Questions