0

I'm coding Entity Framework models and I want to validate incoming data for a given property against a predefined list of allowable values. Based on prior research, I decided the best way to do that is via a customized data annotation attribute, making sure that every property that needs this validation has an accompanying array of values that is passed to this attribute, called "[AllowableValue]"

So I have the following class:

public class className
{
    public int Id { get; set; }

    [Required]
    [AllowableValues(ListOfAllowableValues)]
    [MaxLength(2), MinLength(2)]
    public string propertyName { get; set; }

    [NotMapped]
    public string[] ListOfAllowableValues = new string[]
    {
        "00",
        "77",
        "ZZ"
    };

}

And the following Custom Attribute:

public class AllowableValues : ValidationAttribute
{
    string[] _list;

    public AllowableValues(string[] list)
    {
        _list = list;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (_list.Contains((string)value))
            return ValidationResult.Success;
        return new ValidationResult("Invalid value.");
    }
}

But, In Visual Studio, when I apply the [AllowableValues] attribute, it is giving me the error: "An object reference is required for the non-static field, method, or property 'className.ListOfAllowableValues.'

My definition calls for an array, and I'm passing it an array. Why is it asking for an object reference?

Will
  • 103
  • 12
  • This was a little bit more informative than the first answer. Can you please move this comment down to the answers section so I can accept it? – Will Feb 03 '17 at 16:18
  • Moved it, but am sorry that it doesn't really solve your problem. – René Vogt Feb 03 '17 at 16:26

2 Answers2

2

Attribute declaration must be compile time constant.

So you're in a static context and have no instance of classname. But ListOfAllowableValues is not static but an instance member, so you cannot access it without a classname instance (that is what the error means by "object reference").

Unfortunatly I guess making ListOfAllowableValues static won't help as arguments to attributes must be compile time constant anyway and reference type values cannot be compile time constant.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Thank you. This answer does give me a little better idea of **why** it's not possible, so I'm accepting this one. Thank you very much. – Will Feb 03 '17 at 16:27
1

It is not possible. See here on SO

Can I initialize a C# attribute with an array or other variable number of arguments?

How to: MSDN CustomAttributes

Your only way is to do the following:

[Required]
[AllowableValues(new[]{ "00", "77", "ZZ"})]
[MaxLength(2), MinLength(2)]
public string propertyName { get; set; }
Community
  • 1
  • 1
codeteq
  • 1,502
  • 7
  • 13
  • Useful information. But the requirements for this mean that the list of allowable values can get **very** large when applied to other properties. I was hoping to be able to pass an array to make it all more readable. It looks like I'm going to have to do this via controller code. Thank you though. – Will Feb 03 '17 at 16:24