Paul
Paul

Reputation: 1159

Custom dataannotation enum for property

I want to create a data annotation and I need it to be an enum

something like

public enum DataUsage
{
    Count,
    Average,
    Median,
    Percentage
}

And then slap it onto a property like this (I know the syntax is wrong, I just want to show usage)

[MyDataAnnotation = DataUsage.Average]
public decimal CasesPerYear
{
    get;
    set;
}

Can someone help?

Upvotes: 1

Views: 1764

Answers (1)

Matt B
Matt B

Reputation: 8643

Something like this?

    public enum DataUsage
    {
        Count,
        Average,
        Median,
        Percentage
    }

    public class DataAnnotationAttribute : Attribute
    {
        public DataAnnotationAttribute(DataUsage usage)
        {
            this.Usage = usage;
        }

        public DataUsage Usage { get; private set; }
    }

    [DataAnnotation(DataUsage.Average)]
    public decimal MyProperty { get; set; }        

Upvotes: 2

Related Questions